使用 PHP 更新 SQL 数据库
我正在尝试在我的网站上创建一个密码检索系统,但在更新数据库中的密码重置字段时遇到问题。我已经尝试了一切,但似乎没有任何效果。
到目前为止,这是我的代码:
$passwordreset = md5(mt_rand()) . md5(mt_rand()) . md5(mt_rand());
$con = mysql_connect("localhost","XXX","XXX");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
mysql_query("UPDATE members SET passwordreset = $passwordreset WHERE id = $id");
当我尝试插入数据时,出现错误:
Error: Query was empty
任何帮助将不胜感激, 谢谢。
I am trying to make a password retrieval system on my site, and I am having problems updating the password reset field in my database. I have tried everything, but nothing seems to work.
This is my code so far:
$passwordreset = md5(mt_rand()) . md5(mt_rand()) . md5(mt_rand());
$con = mysql_connect("localhost","XXX","XXX");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
mysql_query("UPDATE members SET passwordreset = $passwordreset WHERE id = $id");
When I try to insert the data I get the error:
Error: Query was empty
Any help would be appreciated,
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是唯一的问题,但我猜你的passwordreset字段是数据库中的一个字符串——为了存储几个md5的串联,这些md5是字符串,它必须这样做。
因此,在 SQL 查询中,您在此字段中输入的值应该用引号引起来:
,在一般情况下,您应该使用
mysql_real_escape_string
对字符串值进行转义:并且 这里不会改变任何东西,因为 md5 中没有引用...但总是这样做是一个很好的做法,永远不会发现自己处于有必要但没有这样做的情况。
Not sure it's the only problem, but I'm guessing your passwordreset field is a string, in the database -- to store a concatenation of several md5, which are strings, it has to.
So, there should be quotes arround the value you put in this field, in the SQL query :
And, in a general case, you should escape your string values with
mysql_real_escape_string
:It won't change anything here, as there is no quote in a md5... But it's a good practice to always do it, to never find yourself in a situation where it was necessary and you didn't do it.
我不确定,如果您收到空查询错误,但您需要在值周围打勾:
I am not sure, if you get an empty query error for this, but you need ticks around the values:
我猜列名称周围的反引号丢失了,请尝试:
I guess the backticks around the names of the columns are missing, try:
$passwordreset
后面的两个换行符是故意的吗?你可以尝试删除它们吗?Are the two line breaks after
$passwordreset
intentional? Can you try removing them?