• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 712
  • Last Modified:

Error in MySQL command

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.
0
Lennart Ericson
Asked:
Lennart Ericson
1 Solution
 
RobRotterdamCommented:
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?
0
 
Lennart EricsonAmateurAuthor 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.
0
 
Ray PaseurCommented:
FROM is a MySQL reserved word.  You might want to enclose that in `backticks` and try the query again.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Julian HansenCommented:
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.
0
 
Ray PaseurCommented:
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
0
 
Lennart EricsonAmateurAuthor Commented:
Ray, Thanks.
// lericson
0
 
Ray PaseurCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now