We help IT Professionals succeed at work.

Error in MySQL command

Medium Priority
821 Views
Last Modified: 2013-11-19
Executing this piece of code:
$SQL = " INSERT INTO lanautbatplats (Datum, Medlnr, Namn, Telefon, Epost, Platsnr, Period, From, Till, Övrigt) VALUES ('$_POST[Datum]', '$_POST[Medlnr]', '$_POST[Namn]', '$_POST[Telefon]', '$_POST[Epost]', '$_POST[Platsnr]', '$_POST[Period]', '$_POST[From]', '$_POST[Till]', '$_POST[Övrigt]') ";
I get this message:
"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 'From, Till, Övrigt) VALUES ('2012-08-13', '12345', 'Lennart Ericson', '085140027' at line 1".

The database sql looks like this:
CREATE TABLE `lanautbatplats` (
  `id` int(11) NOT NULL auto_increment,
  `Datum` varchar(50) default NULL,
  `Medlnr` int(11) default NULL,
  `Namn` varchar(50) default NULL,
  `Telefon` varchar(50) default NULL,
  `Epost` varchar(50) default NULL,
  `Platsnr` varchar(50) default NULL,
  `Period` varchar(50) default NULL,
  `From` varchar(50) default NULL,
  `Till` varchar(50) default NULL,
  `Övrigt` blob,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I can'tsee what is wrong. Please help.
Comment
Watch Question

Could you post an echo  of the actual sql command?
It looks like From/Till and Övrigt are treated as literals.
Why did you create the datum field as a Varchar?

Author

Commented:
RobRotterdam,
Thanks for responding.
1. Could you post an echo  of the actual sql command?
2. It looks like From/Till and Övrigt are treated as literals.
3. Why did you create the datum field as a Varchar?

1. and 2. I don't know/understand what you mean
3. Peaople can write e. g.
2012-08-13 or
2012/08/13 or
20120813 or
13/8/21012 etc.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
FROM is a MySQL reserved word.  You might want to enclose that in `backticks` and try the query again.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Enclose your field names in `` i.e

INSERT INTO lanautbatplats (`Datum`, `Medlnr`, `Namn`, `Telefon`, `Epost`, `Platsnr`, `Period`, `From`, Till, `Övrigt`) 

Open in new window


You have a reserved word FROM as a fieldname and the fieldname Övrigt is also problematic.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Regarding the issues with the date format, please see this article about how to handle DATETIME values in PHP and MySQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Author

Commented:
Ray, Thanks.
// lericson
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks for the points.  Make that "Datum" column be of type DATETIME -- it will save you a lot of headaches.  You can normalize the client input with strtotime() and date().

Best to all, ~Ray

Explore More ContentExplore courses, solutions, and other research materials related to this topic.