使用php插入sql数据库时出错

时间:2014-10-24 17:42:13

标签: php mysql syntax-error sql-insert mysql-error-1064

我有一个带有MySQL插入查询的程序:

        $sql = "INSERT INTO people (person_id, name, username, password, email, salt)
                VALUES ($person_id, $name, $username, $password, $email, $salt)";
        if ($con->query($sql) === TRUE) {
            successful();
        } else {
            unsuccessful("Error: " . $sql . "<br>" . $con->error);
        }

当我运行代码时,我收到此错误:

Error: INSERT INTO people (person_id, name, username, password, email, salt) VALUES (2, Tom Jack, tjack95, 8a01fc598a676a249c5844bd3baf4f4d83cecdf2, tom@tommy.com, eb694539989fda2e17f79e70fb306f8911c3dee5)
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 'Jack, tjack95, 8a01fc598a676a249c5844bd3baf4f4d83cecdf2, tom@tommy.com, eb69' at line 2

sql插件看起来对我很好。有什么问题?

1 个答案:

答案 0 :(得分:4)

您的字符串值周围缺少引号:

$sql = "INSERT INTO people (person_id, name, username, password, email, salt)
            VALUES ($person_id, '$name', '$username', '$password', '$email', '$salt')";