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
Mat
  • 202,337
  • 40
  • 393
  • 406
Joanne
  • 1
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Amal Murali May 04 '14 at 17:34

5 Answers5

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).

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 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()); – Wrenbjor Sep 10 '11 at 12:42
1

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

... mail, `group`, ...
Mat
  • 202,337
  • 40
  • 393
  • 406
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')";

Lars
  • 5,757
  • 4
  • 25
  • 55
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

Marcus
  • 12,296
  • 5
  • 48
  • 66
0

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

CodeCaster
  • 147,647
  • 23
  • 218
  • 272