mysql语法错误怎么办,MySQL语法错误。无法解决

I wanna insert 0 to some db table's fields but can't get it work. The piece of code from my signup php script looks like that.

...

if (isset($type))

{

if($type==1)

{

$region=$data['region'];

$school=$data['school'];

$class=$data['class'];

$group='NULL';

$subject='NULL';

$university='NULL';

$profession='NULL';

}

if($type==2)

{

$group=$data['group'];

$region=$data['region'];

$school=$data['school'];

$class=$data['class'];

$subject='NULL';

$university='NULL';

$profession='NULL';

}

if($type==3)

{

$group='NULL';

$region='NULL';

$school='NULL';

$class='NULL';

$subject='NULL';

$university=$data['university'];

$profession=$data['profession'];

}

if($type==4)

{

$group='NULL';

$region='NULL';

$school='NULL';

$class='NULL';

$university='NULL';

$profession='NULL';

$subject=$data['subject'];

}

}

$sql= "INSERT INTO users

(level,fname, mname, lname, dob, age, reg_date, phone, email, login, pwd, type, group, region, school, class, ip, subject, ban, university, profession)

VALUES

('1','$data[fname]', '$data[mname]', '$data[lname]', '$dob', '$age, now(), '$data[phone]', '$email', '$login', '$pwd', '$data[type]', '$data[region]', '$data[school]', '$data[class]', '$ip', '$subject', NULL, '$university', '$profession')";

$result = $db->query($sql) or die(printf("Errorv: %s\n", $db->error));

$id = $db->insert_id();

$md5_id = md5($id);

$db->query("update users set md5_id='$md5_id' where id='$id'");

// echo "

Thank You

We received your submission.";

...

Getting every time this error message

"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, region, school, class, ip, subject, ban, university, profession) VALUES' at line 2"

Tried '0', 0 instead of NULL. No success. Please help

Changed the code. Still no success

$sql= "INSERT INTO users

(level,fname, mname, lname, dob, age, reg_date, phone, email, login, pwd, type, 'group', region, school, class, ip, subject, ban, university, profession)

VALUES

('1','$data[fname]', '$data[mname]', '$data[lname]', '$dob', '$age', now(), '$data[phone]', '$email', '$login', '$pwd', '$data[type]', '$data[region]', '$data[school]', '$data[class]', '$ip', '$subject', NULL, '$university', '$profession')";

解决方案

Given your edit, you've mis-quoted the word group. YOu can't use single quotes to turn a reserved word into an "acceptable" word, it has to be backticks:

INSERT ....., `group`, ... VALUES ....

^-----^--- note the backticks

Single quotes turn anything into a string, but you can't use a string for a field name.

In the future, if you'r getting an SQL syntax error, show us the actual query that's causing the error. Generally the PHP that's building the query is not necessary - we want to see what MySQL is complaining about. Only after we figure out what the actual problem is can we tell you how to change your code to fix the problem.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值