Page 1 of 1

Parsing strings

Posted: Thu Jun 26, 2008 8:07 pm
by adi5402
I am having a problem with a simple query that i cant work out. I havent done php for a while so its like basic stuff.

I am trying to pass a simple piece of info from a form into a database but there is something wrong with the way i am passing.

The query below works but I get a 0 value for intered as $cost_price

Code: Select all

 
[color=#400040]echo $cost_price =$_POST['bookCostPrice']; 
 
$sql = 'INSERT INTO `price` (`book_ISBN`, `cost_price`, `cost_currancy`, `us_mrp`, `uk_mrp`, `can_mr`) VALUES (\'0750687770\','".$cost_price."', NULL, NULL, NULL, NULL)';
 
mysql_query($sql) or die('Error' . mysql_error());[/color]
['/code']
 
I also tried 
 [color=#0000FF]

Code: Select all

 
$sql = 'INSERT INTO `price` (`book_ISBN`, `cost_price`, `cost_currancy`, `us_mrp`, `uk_mrp`, `can_mr`) VALUES (\'0750687770\', \'".$cost_price."\', NULL, NULL, NULL, NULL)';
 
 
 
mysql_query($sql) or die('Error' . mysql_error());
 
[/color]

BUT I had no luck

Can any one help

thanks

Re: Parsing strings

Posted: Thu Jun 26, 2008 8:51 pm
by Christopher
If the cost_price column is defined as a numeric type then you should not have the quotes around the value.

Re: Parsing strings

Posted: Fri Jun 27, 2008 4:14 am
by WebbieDave
Have you verified that $cost_price contains the expected value after its assignment on line 2? If the value is different than expected, inspect your html form to ensure that the proper method is being used (post) and that the input name is correctly spelled (including case).

Re: Parsing strings

Posted: Fri Jun 27, 2008 4:28 am
by adi5402
Lets persume I am using the cost as a String

I will check it all and get back in a bit. Both queries are working fine but I get the value entered is 0.

I used echo $cost_price to check that the value is being passed properly to the sumbmitted page.

Thanks for looking into this so far.

Adi

Re: Parsing strings

Posted: Fri Jun 27, 2008 4:32 am
by adi5402
I have verified data is being passed ok on line 2

I also tried this modification

Code: Select all

 
 
$sql = 'INSERT INTO `price` (`book_ISBN`, `cost_price`, `cost_currancy`, `us_mrp`, `uk_mrp`, `can_mr`) VALUES (\'0750687770\',\'".$cost_price."\', NULL, NULL, NULL, NULL)';
 
 
Still no luck :cry:

Adi

Re: Parsing strings

Posted: Fri Jun 27, 2008 4:38 am
by WebbieDave
if:

Code: Select all

echo $sql;
yeilds the expected SQL, try copying what is echoed and manually running the SQL through your database client to see if the result is the same.

Re: Parsing strings

Posted: Fri Jun 27, 2008 6:59 am
by adi5402
when i echo the sql query this is what i get

INSERT INTO `price` (`book_ISBN`, `cost_price`, `cost_currancy`, `us_mrp`, `uk_mrp`, `can_mr`) VALUES ('0750687770','".$cost_price."', NULL, NULL, NULL, NULL)

the varible is just not getting passed

Re: Parsing strings

Posted: Fri Jun 27, 2008 8:48 am
by Stryks
Try ...

Code: Select all

$sql = "INSERT INTO `price` (`book_ISBN`, `cost_price`, `cost_currancy`, `us_mrp`, `uk_mrp`, `can_mr`) VALUES ('0750687770', '$cost_price', NULL, NULL, NULL, NULL)";
What do you get?

Re: Parsing strings

Posted: Fri Jun 27, 2008 1:46 pm
by adi5402
Ya that did it I also worked out the String parsing bit the final code that worked is

Code: Select all

 
$sql = 'INSERT INTO `price` (`book_ISBN`, `cost_price`, `cost_currancy`,`us_mrp`,`uk_mrp`,`can_mr`) VALUES (\'0750687770\','.$cost_price.',"'.$cost_currancy.'", '.$us_mrp.', '.$uk_mrp.','.$can_mr.')';
 
 
thanks for your help every one now i can get on with programing