使用 PHP 更新 SQL 数据库

发布于 2024-08-14 16:09:03 字数 507 浏览 3 评论 0原文

我正在尝试在我的网站上创建一个密码检索系统,但在更新数据库中的密码重置字段时遇到问题。我已经尝试了一切,但似乎没有任何效果。

到目前为止,这是我的代码:

$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 技术交流群。

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

发布评论

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

评论(4

得不到的就毁灭 2024-08-21 16:09:03

不确定这是唯一的问题,但我猜你的passwordreset字段是数据库中的一个字符串——为了存储几个md5的串联,这些md5是字符串,它必须这样做。

因此,在 SQL 查询中,您在此字段中输入的值应该用引号引起来:

mysql_query("UPDATE members SET passwordreset = '$passwordreset' WHERE id = $id");

,在一般情况下,您应该使用 mysql_real_escape_string 对字符串值进行转义:

mysql_query("UPDATE members SET passwordreset = '" 
    . mysql_real_escape_string($passwordreset) 
    . "' WHERE id = $id");

并且 这里不会改变任何东西,因为 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 :

mysql_query("UPDATE members SET passwordreset = '$passwordreset' WHERE id = $id");

And, in a general case, you should escape your string values with mysql_real_escape_string :

mysql_query("UPDATE members SET passwordreset = '" 
    . mysql_real_escape_string($passwordreset) 
    . "' WHERE id = $id");

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.

木有鱼丸 2024-08-21 16:09:03

我不确定,如果您收到空查询错误,但您需要在值周围打勾:

mysql_query("UPDATE members SET passwordreset = '$passwordreset' WHERE id = '$id'");

I am not sure, if you get an empty query error for this, but you need ticks around the values:

mysql_query("UPDATE members SET passwordreset = '$passwordreset' WHERE id = '$id'");
只有一腔孤勇 2024-08-21 16:09:03

我猜列名称周围的反引号丢失了,请尝试:

mysql_query("UPDATE members SET `passwordreset` = '$passwordreset' WHERE `id` = '$id'");

I guess the backticks around the names of the columns are missing, try:

mysql_query("UPDATE members SET `passwordreset` = '$passwordreset' WHERE `id` = '$id'");
朮生 2024-08-21 16:09:03

$passwordreset 后面的两个换行符是故意的吗?你可以尝试删除它们吗?

Are the two line breaks after $passwordreset intentional? Can you try removing them?

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