开发者

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 (i.e. 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 is a reserved keyword, you should enclose it in backticks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜