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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!