我应该如何在 mysql_query 函数中编写 PHP $_POST 变量?
在访问我的数据库时,我让用户填写一个表单,然后在目标页面中,发布的值将用于生成的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,注意 SQL 注入!
现在,要回答您的问题,请尝试这样做:
您做错了几件事:
=
运算符而不是LIKE
运算符'
$_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:
You were doing a couple of things wrong:
=
operator instead ofLIKE
operator'
$_POST
array with'
PS: You should use
mysql_real_escape_string()
instead ofmysql_escape_string()
!您只需将变量插入到字符串中,因此将其放入哪个命令并不重要。
有几个问题需要指出。
第一,您可能希望对数组变量使用 {} 格式。在此格式的数组键名称周围不使用引号。
第二,您永远不想进行这样的查询,因为您容易受到 SQL 注入漏洞的影响。考虑一下,如果 $_POST['user'] 是“cow';drop table database;--”怎么办?
您必须先在 POST 输入上运行 mysql_real_escape_string,然后再将其放入查询中,或者使用 进行检查PHP PDO 带有准备好的语句。
格式化字符串并提供一些结构的一种方法是使用 sprintf。
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.
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.
mysql_*()
函数,请始终记住过滤 (mysql_real_escape_string()) 来自不受信任来源(例如用户)的任何数据更多地关注代码的外观。只需比较以下列表即可:
我需要解释哪一个更好阅读、修改或理解吗?
mysql_*()
functions always remember to filter (mysql_real_escape_string()) any data that comes from untrusted source (like user)Pay more attention to how your code looks like. Just compare the following listings:
Do I have to explain which one is better to read, modify or understand?
为什么不检查一下 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.
尝试一下
,
清理通过 $_GET 或 $_POST 收到的任何内容总是一个好主意
Try
and
Its always a good idea to sanitize anything received through $_GET or $_POST