MySQL语法错误.无法解决 [英] MySQL Syntax error. Can't solve it

查看:183
本文介绍了MySQL语法错误.无法解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据库表的某些字段中插入0,但无法正常工作.我的注册php脚本中的这段代码看起来像那样.

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 "<h3>Thank You</h3> We received your submission.";

...

每次获取此错误消息 您的SQL语法有错误;请在与MySQL服务器版本相对应的手册中找到正确的语法,以在行,'组,地区,学校,班级,ip,主题,禁令,大学,专业)VALUES'附近使用2"

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"

尝试为"0",而不是NULL为0.没有成功请帮助

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

更改了代码.仍然没有成功

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

推荐答案

鉴于您所做的编辑,您错误地引用了单词group.您不能使用单引号将保留字变成可接受"字,它必须是反引号:

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.

将来,如果您遇到SQL语法错误,请向我们显示导致该错误的实际查询.通常,构建查询的PHP是不必要的-我们想看看MySQL抱怨什么.只有在弄清实际问题是什么之后,我们才能告诉您如何更改代码以解决问题.

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.

这篇关于MySQL语法错误.无法解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆