Seaholme Posted September 2, 2010 Share Posted September 2, 2010 Hey all, I keep getting this error: 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 2 When I use the script below. I'm finding it a bit confusing because everything about it continues to work, it's just it gives me an error. When the script runs, the outcome is "Success! Your dog now looks far more energetic! Looks like this food is all used up." Followed by the error, which cuts the remainder of the page off. Does anybody know why it's doing this? $dogyay = $_POST['dogid']; $checkenergy = "SELECT energy FROM dogs WHERE id=$dogyay"; $energylevel = mysql_query($checkenergy) or die(mysql_error()); $row = mysql_fetch_array($energylevel) or die(mysql_error()); if($row['energy'] >= 100) { echo "<b>Oops!</b> Looks like your dog is full right now...";} else{ echo "<b>Success! Your dog now looks far more energetic!</b><br><br>"; $sql11="UPDATE dogs SET energy=energy + 50 WHERE id=$dogyay"; $result11=mysql_query($sql11); $sql12="UPDATE items SET uses = uses - 1 WHERE itemid=$id"; $result12=mysql_query($sql12); $checkuses = "SELECT uses FROM items WHERE itemid=$id"; $useslevel = mysql_query($checkuses) or die(mysql_error()); $row = mysql_fetch_array($useslevel) or die(mysql_error()); if($row['uses'] == 0) { echo "Looks like this food is all used up.<bR><br>"; mysql_query("DELETE FROM items WHERE itemid='$id'") or die(mysql_error());} Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/ Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Where is the variable $id defined? Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/#findComment-1106537 Share on other sites More sharing options...
Seaholme Posted September 2, 2010 Author Share Posted September 2, 2010 Ah elsewhere in the script, it comes from the URL as a $_GET['id'] type thing. I've checked all of the variables by echoing them out and they do -- also the script itself functions perfectly, which it wouldn't do if any of the variables were wrong. That's kinda why I'm very confused! Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/#findComment-1106540 Share on other sites More sharing options...
sasa Posted September 2, 2010 Share Posted September 2, 2010 is variable $id have value? Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/#findComment-1106545 Share on other sites More sharing options...
Seaholme Posted September 2, 2010 Author Share Posted September 2, 2010 Yep, it does. It's a number to say which item it is. I've been thinking about it and I was wondering, is it possible that because one of those lines deletes the item from the database that that is giving the error? Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/#findComment-1106548 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Yep, it does. It's a number to say which item it is. I've been thinking about it and I was wondering, is it possible that because one of those lines deletes the item from the database that that is giving the error? What do you mean? Variables used within a query will not be unset. So no it shouldn't affect the $id variable To find out which query is causing the error . Change the or die(myql_error()); bits to include your queries, eg $checkenergy = "SELECT energy FROM dogs WHERE id=$dogyay"; $energylevel = mysql_query($checkenergy) or die($checkenergy . '<br />' . mysql_error()); That way you will know which query the error refers to and it'll stop use from asking silly questions. Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/#findComment-1106550 Share on other sites More sharing options...
samshel Posted September 2, 2010 Share Posted September 2, 2010 mysql_query("DELETE FROM items WHERE itemid='$id'") or die(mysql_error());} try changing to: mysql_query("DELETE FROM items WHERE itemid=$id") or die(mysql_error());} remove the single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/212376-unknown-mysql-syntax-error/#findComment-1106551 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.