mysql error. HELP!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

mysql error. HELP!

Post by m2babaey »

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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

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 »

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());  
:? :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

What are you saying?
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

sorry
DO you know what i learnt today?
that ` is diffrernt from ' :lol: :lol:
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

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!
Post Reply