Jump to content


Photo

Small Error


  • Please log in to reply
9 replies to this topic

#1 JamesRyzon

JamesRyzon
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 September 2005 - 10:30 PM

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?

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 15 September 2005 - 10:41 PM

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--]
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 JamesRyzon

JamesRyzon
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 September 2005 - 02:56 AM

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.

#4 czambran

czambran
  • Staff Alumni
  • Advanced Member
  • 52 posts
  • LocationFairfield,IA

Posted 16 September 2005 - 03:10 AM

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.

Chris
Using undefined variables in PHP is not an error, just sloppy coding practice. PHP5 Power Programming BOOK
--------------------------------------------------------------------------
PHP F.A.Q.
Linux User #394154

#5 JamesRyzon

JamesRyzon
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 September 2005 - 03:19 AM

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.

View Post


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");


#6 czambran

czambran
  • Staff Alumni
  • Advanced Member
  • 52 posts
  • LocationFairfield,IA

Posted 16 September 2005 - 03:26 AM

change:
$sqlstring = "INSERT INTO journal(desc) VALUES('$desc')";

to
$sqlstring = "INSERT INTO journal (`ID`,`desc`) VALUES('','$desc')";

Chris
Using undefined variables in PHP is not an error, just sloppy coding practice. PHP5 Power Programming BOOK
--------------------------------------------------------------------------
PHP F.A.Q.
Linux User #394154

#7 JamesRyzon

JamesRyzon
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 September 2005 - 03:31 AM

change:

$sqlstring = "INSERT INTO journal(desc) VALUES('$desc')";

to
$sqlstring = "INSERT INTO (`journal`,`desc`) VALUES('','$desc')";

View Post


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')



#8 czambran

czambran
  • Staff Alumni
  • Advanced Member
  • 52 posts
  • LocationFairfield,IA

Posted 16 September 2005 - 03:37 AM

oops I made a mistake on my previous post. I have corrected it please try it again.
Chris
Using undefined variables in PHP is not an error, just sloppy coding practice. PHP5 Power Programming BOOK
--------------------------------------------------------------------------
PHP F.A.Q.
Linux User #394154

#9 JamesRyzon

JamesRyzon
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 September 2005 - 03:40 AM

oops I made a mistake on my previous post. I have corrected it please try it again.

View Post


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!

#10 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 16 September 2005 - 02:35 PM

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!

View Post

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users