简体   繁体   中英

Why can't i insert into mysql database using php?

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

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 (ie each variable is being sanitized before being inserted into the code).

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

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

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')";

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

group是保留关键字,应将其括在反引号中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM