这个php脚本有什么问题?

发布于 2024-11-05 05:27:39 字数 263 浏览 8 评论 0原文

我在这一行中不断收到错误 1064:

$sqlquery = "INSERT INTO user 
               (username, password, email, key) 
             VALUES 
                ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."','".$activation."')";`

I keep getting error 1064 in this line:

$sqlquery = "INSERT INTO user 
               (username, password, email, key) 
             VALUES 
                ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."','".$activation."')";`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆沫 2024-11-12 05:27:39

key 是您在查询中使用的保留字,必须使用反引号进行转义。保留字错误为 1064。

您还应该考虑学习一些安全理论,特别是关于在查询中使用未转义值(直接来自用户)。

下面的代码既安全又固定:

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
$sqlquery = "INSERT INTO `user` (`username`, `password`, `email`, `key`) VALUES ('{$username}','{$password}','{$email}','{$activation}')";

涉及查询(好吧,任何事情)时的一个简单规则是永远不要信任用户输入。通过使用 mysql_real_escape_string ,您可以转义变量,以便将它们安全地插入到数据库中。如果没有它,您可以允许用户运行他们想要的任何查询。

为了供将来参考,这里有一个MySQL 保留字的完整列表

key is a reserved word which you're using in your query, this must be escaped with backticks. Reserved word error is 1064.

You should also consider learning some security theory particularly with regards to using unescaped values in a query (straight from a user).

The below code is both secure and fixed:

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
$sqlquery = "INSERT INTO `user` (`username`, `password`, `email`, `key`) VALUES ('{$username}','{$password}','{$email}','{$activation}')";

A simple rule when it comes to queries (well, anything) is to never trust user input. By using mysql_real_escape_string you're escaping the variables so that they're safe for insertion into the database. Without it, you could allow the user to run any query that they wanted to.

For future reference, here is a complete list of MySQL Reserved Words.

小巷里的女流氓 2024-11-12 05:27:39

MySQL错误1064通常意味着SQL语法错误。查看您的 SQL 语句以确保其有效。

调试此类错误的一个好方法是打印出 SQL,然后尝试在 MySQL 中手动执行它。

MySQL error 1064 generally means a SQL syntax error. Take a look at your SQL statement to make sure it's valid.

A good way to debug those kinds of errors is to print out the SQL, then try to execute it manually in MySQL.

蓝咒 2024-11-12 05:27:39

如果你改用这个,你还会得到错误吗:

$query = sprintf("INSERT INTO user 
                    (username, password, email, `key`) 
                  VALUES 
                    ('%s','%s','%s','%s')",
                  mysql_real_escape_string($_POST["username"]),
                  mysql_real_escape_string($_POST["password"]),
                  mysql_real_escape_string($_POST["email"]),
                  mysql_real_escape_string($_POST["activation"]));

$result = mysql_query($query);

KEY is a MySQL保留字——它需要用反引号括起来以避免在查询中使用。如果不使用保留字,则不需要反引号......

Do you still get errors if you use this instead:

$query = sprintf("INSERT INTO user 
                    (username, password, email, `key`) 
                  VALUES 
                    ('%s','%s','%s','%s')",
                  mysql_real_escape_string($_POST["username"]),
                  mysql_real_escape_string($_POST["password"]),
                  mysql_real_escape_string($_POST["email"]),
                  mysql_real_escape_string($_POST["activation"]));

$result = mysql_query($query);

KEY is a MySQL reserved word -- it needs to be enclosed in backticks to escape its use in queries. Backticks are not necessary if not using reserved words...

饮惑 2024-11-12 05:27:39

尝试用 mysql_real_escape_string() 包围每个变量 $var,例如

代替 $_POST["password"]
使用mysql_real_escape_string($_POST["password"])

Try surrounding each variable $var with mysql_real_escape_string(), such as

instead of $_POST["password"]
use mysql_real_escape_string($_POST["password"])!

何止钟意 2024-11-12 05:27:39

直接从 HTTP 请求中获取用户定义的值并将它们连接到 SQL 查询中是不好,并且可能是语法错误的根源。确保转义所有值。

Taking user-defined values directly from the HTTP Request and concatenating them into an SQL query is B-A-D, and likely the source of your syntax error. Make sure you escape all values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文