Jump to content

Recommended Posts

I have this code...it was working when I created the site like 3 months ago, but now it does not work. I get an error that says this "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1". Any help would be much appreciated.

 

$res = mysql_query("SELECT * FROM `guide_order` WHERE user_id = '$userid' ORDER BY priority") or die('Error, query failed');
while($r = mysql_fetch_array($res)){
$db->query("INSERT INTO `order` (`order_number`,`pre_order_id` , `item_id` , `amount` , `user_id` )  VALUES (".$num.",".$r['id'].", ".$r['item_id'].", ".$r['amount'].", ".$r['user_id'].")") or die (mysql_error());

Change this:

"INSERT INTO `order` (`order_number`,`pre_order_id` , `item_id` , `amount` , `user_id` )  VALUES (".$num.",".$r['id'].", ".$r['item_id'].", ".$r['amount'].", ".$r['user_id'].")"

 

To this:

 

"INSERT INTO `order` (`order_number`,`pre_order_id` , `item_id` , `amount` , `user_id` )  VALUES ('".$num."','".$r['id']."', '".$r['item_id']."', '".$r['amount']."', '".$r['user_id']."')"

If your fields are numeric, that change will only have the effect of inserting null/default values into the table. If you are expecting data values for each field, you need to find out why they are not being set by your script.

If you don't use quotes around your values, MySQL will think that it is a command, so try and get in the habit of doing that.

 

Not exactly true, numerical data, as PF pointed out, will be just fine. Yes, string values need to be surrounded with single quotes, or it is just in-proper syntax, and throws a syntax error. But from the error, it seems as though a value should be there, that is not. Which is due to poor validation techniques.

 

As far as MySQL thinking it is a command, may be, but not all data needs 'single-quotes' around it :)

 

 

I always do it, what can it hurt?

 

I believe mysql will always treat the data as as an int even though it is sent as a string. I have had no problems doing math on the values afterward...

 

I am not saying it will cause problems, you can, by all means do it. Whether it is required or needed, no not really. And doing it without really fully understanding why could cause someone problems.

 

As for me, I prefer to leave int's outside of quotes, so incase my form does screw up I get an error message about it. But that is just me being particular.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.