JamesRyzon Posted September 15, 2005 Share Posted September 15, 2005 Ok ive been at this myself for some time Im having an error inputting to a database, very simple error i assume but i cant seem to fix it (Even when i copy and paste a query from another page which i am POSITIVE works, ive even used phpmyadminl to generate code for me which worked but not inserting my variable) $desc = " ".$mons." defeats ".$name." in defence"; $sql9 = mysql_query("INSERT INTO journal(desc) VALUES('$desc')")or die(mysql_error()); The table has two rows, ID which is set to auto_increment, and desc, which is set to text. i get the following error 10/10 times - u have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near... after 'near' it shows the data that should be appended to $desc, and sent, but nothing is, sigh, any help? Quote Link to comment Share on other sites More sharing options...
effigy Posted September 15, 2005 Share Posted September 15, 2005 try this: [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$desc = mysql_escape_string(\" $mons defeats $name in defence\"); $sql9 = mysql_query(\"INSERT INTO journal(desc) VALUES(\'$desc\')\")or die(mysql_error());[/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Quote Link to comment Share on other sites More sharing options...
JamesRyzon Posted September 16, 2005 Author Share Posted September 16, 2005 Tried even copy and pasting it as you have and i still recieve the same error. Ive also tried switching types to compensate and no luck. Quote Link to comment Share on other sites More sharing options...
czambran Posted September 16, 2005 Share Posted September 16, 2005 change: $sql9 = mysql_query("INSERT INTO journal(desc) VALUES('$desc')")or die(mysql_error()); to $sqlstring = "INSERT INTO journal(desc) VALUES('$desc')"; $sql9 = mysql_query($sqlstring)or die(mysql_error() . " in the query $sqlstring"); and post back your new error message. Quote Link to comment Share on other sites More sharing options...
JamesRyzon Posted September 16, 2005 Author Share Posted September 16, 2005 change: $sql9 = mysql_query("INSERT INTO journal(desc) VALUES('$desc')")or die(mysql_error()); to $sqlstring = "INSERT INTO journal(desc) VALUES('$desc')"; $sql9 = mysql_query($sqlstring)or die(mysql_error() . " in the query $sqlstring"); and post back your new error message. 295980[/snapback] The entire error i got is 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 'desc) VALUES(' DaRkNeSs defeats James3 in defence')' at line 1 in the query INSERT INTO journal(desc) VALUES(' DaRkNeSs defeats James3 in defence') the code im using now $desc = $mons.' defeats '.$name.' in defence'; $sqlstring = "INSERT INTO journal(desc) VALUES('$desc')"; $sql9 = mysql_query($sqlstring)or die(mysql_error() . " in the query $sqlstring"); Quote Link to comment Share on other sites More sharing options...
czambran Posted September 16, 2005 Share Posted September 16, 2005 change: $sqlstring = "INSERT INTO journal(desc) VALUES('$desc')"; to $sqlstring = "INSERT INTO journal (`ID`,`desc`) VALUES('','$desc')"; Quote Link to comment Share on other sites More sharing options...
JamesRyzon Posted September 16, 2005 Author Share Posted September 16, 2005 change: $sqlstring = "INSERT INTO journal(desc) VALUES('$desc')"; to $sqlstring = "INSERT INTO (`journal`,`desc`) VALUES('','$desc')"; 295987[/snapback] Recieving the same error pretty much 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 '(`journal`,`desc`) VALUES('',' DaRkNeSs defeats James3 in defen in the query INSERT INTO (`journal`,`desc`) VALUES('',' DaRkNeSs defeats James3 in defence') Quote Link to comment Share on other sites More sharing options...
czambran Posted September 16, 2005 Share Posted September 16, 2005 oops I made a mistake on my previous post. I have corrected it please try it again. Quote Link to comment Share on other sites More sharing options...
JamesRyzon Posted September 16, 2005 Author Share Posted September 16, 2005 oops I made a mistake on my previous post. I have corrected it please try it again. 295990[/snapback] Thank you that worked, is there any reason it was not auto_incrementing the ID on its own? Or is this something about SQL I was unaware of (When adding only one value, it messes up?) Just trying to get some info on this so it dosent happen again. Thanks again! Quote Link to comment Share on other sites More sharing options...
shoz Posted September 16, 2005 Share Posted September 16, 2005 Thank you that worked, is there any reason it was not auto_incrementing the ID on its own? Or is this something about SQL I was unaware of (When adding only one value, it messes up?) Just trying to get some info on this so it dosent happen again. Thanks again! 295991[/snapback] The error was caused by the use of "desc" in your query. "desc" is a special keyword in MYSQL and if used as a column or table name should be surrounded by backticks(`) when referenced in a query as Czambran's example shows. Quote Link to comment 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.