我插入到行有一个错误,这是关于这个坏男孩
mysql_query("INSERT INTO accounts('username', 'password', 'firstname', 'lastname', 'email')
VALUES($username, $password, $firstname, $lastname, $email)")
or die("Could not create account! <br/>" . mysql_error());
给出的错误如下:
无法创建帐户!
你有一个错误在你的SQL语法;检查与你的MySQL服务器版本对应的手册,以便在"用户名","密码","姓","姓","电子邮件"附近使用正确的语法)VALUES(test, test,' at line 1
我怀疑这与变量没有被正确调用有关?
有各种各样的问题,所以我将总结它们:
-
INSERT INTO t1 ('col'
——注意'col'
是用引号括起来的。这意味着它尝试插入字符串文字"col1"而不是列名。删除引号并将其替换为反引号(或不替换) - 值本身没有用引号括起来。您有
VALUES(test
—这在语义上意味着插入列"test"的值,这没有意义。你需要用引号把这个括起来。 - 我冒昧地猜测没有一个输入参数被正确转义。你应该在PDO或mysqli中使用适当的参数化查询。
朋友,查询中的字段不应该加引号。试试这个:
mysql_query("INSERT INTO accounts (username, password, firstname, lastname, email) VALUES($username, $password, $firstname, $lastname, $email)") or die("Could not create account!" . mysql_error());
好运您不必在查询中使用引号'
,但反勾'
mysql_query("INSERT INTO `accounts` (`username`, `password`, `firstname`, `lastname`, `email`) VALUES('".$username."', '".$password."','". $firstname."', '".$lastname."', '".$email."')") or die("Could not create account! " . mysql_error())
插入文档
我还想提醒您,mysql_
函数已被弃用,因此我建议您在新项目中切换到mysqli
或PDO
。
mysql_query不推荐使用,即将弃用。可能最好使用PDO或mysqli代替。http://php.net/manual/en/function.mysql-query.php
但是回答你的问题,我相信这是因为你的列名是在单引号(而不是反引号)。
你的值需要用单引号括起来。您可能还需要在这些变量上运行mysql_real_escape_string,以防止SQL注入。或者直接使用PDO预处理语句。http://php.net/manual/en/pdo.prepared-statements.php