Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
m2babaey
Forum Contributor
Posts: 364 Joined: Sun May 20, 2007 9:26 am
Post
by m2babaey » Thu Jul 19, 2007 9:57 pm
Hi.
This code prompts an error. How can I fix it?
Code: Select all
mysql_query("INSERT INTO details ( date, time, where ,who, price, action ,comments ) VALUES('$date','$time','$where','$who','$price' ,'$action' ,'$comments' )")
or die(mysql_error());
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 'where ,who, price, action ,comments ) VALUES('07/14/2007','0940
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 19, 2007 10:04 pm
Code: Select all
mysql_query("INSERT INTO `details` (`date`, `time`, `where` ,`who`, `price`, `action` ,`comments`) VALUES('" . mysql_real_escape_string($date) . "','" . mysql_real_escape_string($time) . "','" . mysql_real_escape_string($where) . "','" . mysql_real_escape_string($who) . "','" . mysql_real_escape_string($price) . "' ,'" . mysql_real_escape_string($action) . "' ,'" . mysql_real_escape_string($comments) . "' )")
OR die(mysql_error());
m2babaey
Forum Contributor
Posts: 364 Joined: Sun May 20, 2007 9:26 am
Post
by m2babaey » Thu Jul 19, 2007 10:15 pm
I solved it by this code:
Code: Select all
mysql_query("INSERT INTO 'details' ( 'date', 'time', 'where' ,'who', 'price', 'action' ,'comments' ) VALUES('$date','$time','$where','$who','$price' ,'$action' ,'$comments' )")
or die(mysql_error());
But I had another code already like below and it was ( and is ) working
Code: Select all
mysql_query("INSERT INTO articles (section, title, text ,username, name ) VALUES('$subject','$title','$article','$username','$name' )")
or die(mysql_error());
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 19, 2007 10:38 pm
What are you saying?
m2babaey
Forum Contributor
Posts: 364 Joined: Sun May 20, 2007 9:26 am
Post
by m2babaey » Thu Jul 19, 2007 11:32 pm
sorry
DO you know what i learnt today?
that ` is diffrernt from '
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Fri Jul 20, 2007 6:05 am
m2babaey wrote: I solved it by this code:
Code: Select all
mysql_query("INSERT INTO 'details' ( 'date', 'time', 'where' ,'who', 'price', 'action' ,'comments' ) VALUES('$date','$time','$where','$who','$price' ,'$action' ,'$comments' )")
or die(mysql_error());
But I had another code already like below and it was ( and is ) working
Code: Select all
mysql_query("INSERT INTO articles (section, title, text ,username, name ) VALUES('$subject','$title','$article','$username','$name' )")
or die(mysql_error());
First fragment consist reserved MySQL words ('where' for example) which you should put into `...` to avoid the error
califdon
Jack of Zircons
Posts: 4484 Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA
Post
by califdon » Fri Jul 20, 2007 4:31 pm
Gente wrote: First fragment consist reserved MySQL words ('where' for example) which you should put into `...` to avoid the error
Better yet,
don't use reserved words as names for variables or fields!