March 30, 2011
What is wrong with this MySQL syntax?
Question by tekknolagi
INSERT INTO homework_MarcWed30-2011 ('Teacher', 'Class', 'Period', 'Assn')
VALUES ('a', 'a', 'a', 'a')
i get the following 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 ‘-2011 (Teacher varchar(30), Class varchar(30), Period varchar(30), Assn varchar(‘ at line 1
what is going on?
on a side note, the create table statement does not work for me:
mysql_query("CREATE TABLE homework_MarcWed30-2011 (Teacher varchar(30), Class varchar(30), Period varchar(30), Assn varchar(400))","mysql_connect('#####', '#####', '#####')") OR die(mysql_error());
Answer by Positive
Column names should be out of quotes they can be surrounded by back quote “ but not with single or double quote.
INSERT INTO `homework_MarcWed30-2011` (`Teacher`, `Class`, `Period`, `Assn`)
VALUES ('a', 'a', 'a', 'a')
if there is – in table name you can still use by surrounding name in back quotes “.
Answer by Starx
Try this query
INSERT INTO `homework_marcwed30-2011` (`Teacher` ,`Class` ,`Period` ,`Assn`) VALUES ('a', 'a', 'a', 'a');
Variation, without field quotes
INSERT INTO `homework_marcwed30-2011` (Teacher ,Class ,Period ,Assn)
VALUES ('a', 'a', 'a', 'a')
Variation, without field defination
INSERT INTO `homework_marcwed30-2011` VALUES ('a', 'a', 'a', 'a')
For the table creation part
mysql_query("CREATE TABLE `homework_MarcWed30-2011` (Teacher varchar(30) NOT NULL, Class varchar(30) NOT NULL, Period varchar(30) NOT NULL, Assn varchar(400) NOT NULL);","mysql_connect('#####', '#####', '#####')") OR die(mysql_error());
Your error is that you didn’t define whether to allow Null or not.