我应该如何在 mysql_query 函数中编写 PHP $_POST 变量?

发布于 2024-08-18 18:09:27 字数 635 浏览 7 评论 0原文

在访问我的数据库时,我让用户填写一个表单,然后在目标页面中,发布的值将用于生成的 MySQL 查询中。

$query = mysql_query("SELECT pass FROM database WHERE user='$_POST[user]'");

然而,由于某种原因,MySQL 不喜欢我在命令中使用 $_POST 变量,并且只有在我定义(例如) $user = $_POST['user'];$user = $_POST['user'];,然后直接将$user放入SQL命令中。

另一方面,我可以在不需要特定列名的 INSERT 语句中使用 $_POST 值:

$query = mysql_query("INSERT INTO database VALUES ('foo', 'bar', '$_POST[user]'");

如果我尝试定义属性的 INSERT 语句(例如 user='foo'),则出现同样的问题。

我在 SQL 查询中做错了什么,导致命令在运行时出错,但可以使用格式化 INSERT 命令的特定方法?

希望这不是“运气不好,看起来你必须分配所有发布的值”。呵呵。

In accessing my database, I have the user fill out a form, and in the target page, the posted values are used in the resulting MySQL query.

$query = mysql_query("SELECT pass FROM database WHERE user='$_POST[user]'");

However, for some reason or another, MySQL doesn't like my using a $_POST variable in the command, and it only works if I define (for example) $user = $_POST['user'];, and then put $user directly in the SQL command.

On the other hand, I can use $_POST values in INSERT statements where specific column names are not required:

$query = mysql_query("INSERT INTO database VALUES ('foo', 'bar', '$_POST[user]'");

If I try an INSERT statement where attributes are defined (e.g. user='foo'), then the same problem appears.

What am I doing wrong in my SQL query that causes the command to error out when run, but works with the specific method of formatting an INSERT command?

Hopefully, it's not "tough luck, looks like you have to assign all of your posted values". Heh.

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

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

发布评论

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

评论(5

您的好友蓝忘机已上羡 2024-08-25 18:09:27

首先,注意 SQL 注入

现在,要回答您的问题,请尝试这样做:

$query = mysql_query("SELECT `pass` FROM `database` WHERE `user` LIKE '" . mysql_escape_string($_POST['user']) . "';");

您做错了几件事:

  • 使用 = 运算符而不是 LIKE 运算符
  • ,未将值包含在 SQL 查询中with '
  • 未将用户索引包含在 $_POST 数组中 with '

PS:您应该使用 mysql_real_escape_string() 而不是 mysql_escape_string()

First of, watch out for SQL Injections!

Now, to answer your question try doing this instead:

$query = mysql_query("SELECT `pass` FROM `database` WHERE `user` LIKE '" . mysql_escape_string($_POST['user']) . "';");

You were doing a couple of things wrong:

  • using the = operator instead of LIKE operator
  • not enclosing the value in the SQL query with '
  • not enclosing the user index in the $_POST array with '

PS: You should use mysql_real_escape_string() instead of mysql_escape_string()!

一桥轻雨一伞开 2024-08-25 18:09:27

您只需将变量插入到字符串中,因此将其放入哪个命令并不重要。

有几个问题需要指出。

第一,您可能希望对数组变量使用 {} 格式。在此格式的数组键名称周围不使用引号。

$query = mysql_query("SELECT pass FROM database WHERE user='{$_POST[user]}'")

第二,您永远不想进行这样的查询,因为您容易受到 SQL 注入漏洞的影响。考虑一下,如果 $_POST['user'] 是“cow';drop table database;--”怎么办?

您必须先在 POST 输入上运行 mysql_real_escape_string,然后再将其放入查询中,或者使用 进行检查PHP PDO 带有准备好的语句。

格式化字符串并提供一些结构的一种方法是使用 sprintf。

$query=mysql_query(sprintf("SELECT pass FROM database WHERE user='%s'",mysql_real_escape_string($_POST['user'])));

You're simply inserting a variable into a string, so it shouldn't matter which command you're putting it into.

There are a few issues to point out.

One, you might want to use the {} format for array variables. You don't use quotes around the arrray key names in this format.

$query = mysql_query("SELECT pass FROM database WHERE user='{$_POST[user]}'")

Two, you'd never want to make a query like that because you are open to sql injection holes. Consider, what if $_POST['user'] was "cow';drop table database;--"?

You must either run mysql_real_escape_string on the POST input before putting it into your query, or check out using PHP PDO with prepared statements.

One way to do format your string which provides a bit of structure is to use sprintf.

$query=mysql_query(sprintf("SELECT pass FROM database WHERE user='%s'",mysql_real_escape_string($_POST['user'])));
装纯掩盖桑 2024-08-25 18:09:27
  1. 使用 PDO - 它提供了更好的 API 来与数据库通信。
  2. 如果您使用 mysql_*() 函数,请始终记住过滤 (mysql_real_escape_string()) 来自不受信任来源(例如用户)的任何数据
  3. 更多地关注代码的外观。只需比较以下列表即可:

    $query = mysql_query("INSERT INTO database VALUES ('foo', 'bar', " . mysql_real_escape_string($_POST['user']) . ", " . mysql_real_escape_string($_POST['user') ]) . mysql_real_escape_string($_POST['user']) ", " .
    
    $query = sprinf('插入数据库值("foo", "bar", "%s", "%s", "%s")',
    mysql_real_escape(...), ...);
    

    我需要解释哪一个更好阅读、修改或理解吗?

  1. Use PDO - it provides much better API to communicate with DB.
  2. If you're using mysql_*() functions always remember to filter (mysql_real_escape_string()) any data that comes from untrusted source (like user)
  3. Pay more attention to how your code looks like. Just compare the following listings:

    $query = mysql_query("INSERT INTO database VALUES ('foo', 'bar', " . mysql_real_escape_string($_POST['user']) . ", " . mysql_real_escape_string($_POST['user']) . ", " . mysql_real_escape_string($_POST['user']) . ", " . mysql_real_escape_string($_POST['user']) . ")");
    
    $query = sprinf('INSERT INTO database VALUES ("foo", "bar", "%s", "%s", "%s")',
    mysql_real_escape(...), ...);
    

    Do I have to explain which one is better to read, modify or understand?

稀香 2024-08-25 18:09:27

为什么不检查一下 mysql_error() 对此有何说明呢?如果您的查询无效,mysql_error() 将返回一个漂亮的文本块,告诉您到底出了什么问题。

至于 MySQL 不喜欢 POST var,如果您在某些运行中直接插入 POST var,但在其他运行中则不然,那么您应该确保每次测试都使用一致的数据和设置。如果使用 GET 完成某些测试,那么您的 POST 变量将为空。如果您为每个测试使用不同的用户名,请查看失败的用户名之间是否一致。

如上所述,请阅读有关 SQL 注入以及您的查询如何乞求被恶意用户破坏的信息。

Why not check and see what mysql_error() has to say about it? If your query is invalid, mysql_error() will return a nice blob of text telling you exactly what went wrong.

As for MySQL not liking the POST var if you insert it directly for some runs, but not others, then you should make sure you're using consistent data and setups for each test. If some test are done using a GET, then your POST vars will be empty. If you're using different user names for each test, then see if what's consistent between the ones that fail.

And as mentioned above, read up about SQL injection and how your query is just begging to be subverted by a malicious user.

段念尘 2024-08-25 18:09:27

尝试一下

$query = mysql_query("SELECT pass FROM database WHERE user=" . mysql_real_escape_string($_POST['user']));

$query = mysql_query("INSERT INTO database VALUES ('foo', 'bar', " . mysql_real_escape_string($_POST['user']) . ")");

清理通过 $_GET 或 $_POST 收到的任何内容总是一个好主意

Try

$query = mysql_query("SELECT pass FROM database WHERE user=" . mysql_real_escape_string($_POST['user']));

and

$query = mysql_query("INSERT INTO database VALUES ('foo', 'bar', " . mysql_real_escape_string($_POST['user']) . ")");

Its always a good idea to sanitize anything received through $_GET or $_POST

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