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.