Parsing strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Parsing strings

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Parsing strings

Post by Christopher »

If the cost_price column is defined as a numeric type then you should not have the quotes around the value.
(#10850)
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Parsing strings

Post 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).
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Re: Parsing strings

Post 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
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Re: Parsing strings

Post 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
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Parsing strings

Post 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.
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Re: Parsing strings

Post 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
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Parsing strings

Post 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?
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Re: Parsing strings

Post 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
Post Reply