0

this is my code:

 $query = "INSERT INTO accounts (name, mail, group, year, active, password) VALUES  ('$name', '$email', '$group', '$year', '1', '$pass')";          
 $result = mysql_query($query) or DIE(mysql_error());

this is my DB

Field -  Type 
id       int(11) (auto increment)
name     varchar(30)
mail     varchar(30)
group    varchar(4)
year     varchar(3)
active   tinyint(1)
password varchar(36)

and i get this 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 'group, year, active, password) VALUES ('Smith S. Ana Joy', 'ana' at line 1

an example or the variables that i am adding:

$name = Smith
$email = ana.smith
$group = A1
$year = I
$active = 1
$pass = a
1

5 Answers 5

3

GROUP is a reserved word in mySQL.

Wrap it in backticks:

`group`

or use a different column name.

Also make sure your code is protected against SQL injection (i.e. each variable is being sanitized before being inserted into the code).

Sign up to request clarification or add additional context in comments.

1 Comment

Always quote your queries. you have a few keywords in there, "group", "password", and "year" are all mysql keywords. $query = "INSERT INTO accounts (name, mail, group, year, active, password) VALUES ('$name', '$email', '$group', '$year', '1', '$pass')"; $result = mysql_query($query) or DIE(mysql_error());
1

GROUP is a reserved word. You should quote that column name:

... mail, `group`, ...

Comments

1

group is a MYSQL-command and may not be used like that in a value. Escape the field like so:

"INSERT INTO accounts (name, mail, \"group\", year, active, password) VALUES ('$name', '$email', '$group', '$year', '1', '$pass')";

Comments

1

Group is a reserverd word in MySQL and therefore you must qoute it. You can read more about it here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Comments

0

group is a reserved keyword, you should enclose it in backticks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.