1

我正在尝试编辑 Mysql 数据库。有人可以告诉我为什么这不起作用,它没有更新任何东西

mysqli_query($con,"UPDATE Users SET day_started=1 WHERE email='$user_data['email']'");

$user_data['email'] 工作我检查了它。我尝试回应它,它确实回应了我想要的价值。我还检查了数据库以及我想要的电子邮件字段中的值。

谢谢您的帮助:)

4

3 回答 3

4

尝试这个:

$user_email = mysqli_real_escape_string($con, $user_data['email']);

mysqli_query($con,"UPDATE `Users` SET `day_started`='1' WHERE `email`='".$user_email."'");

我怀疑答案在于您没有在查询字符串中正确嵌入 PHP 变量。看看每一侧的那些性感的句号$user_email。当你这样做时,PHP 喜欢它。

我还清理了您的输入和内容,并用反引号格式化了您的查询,因为 PHP 也喜欢这样。

于 2013-08-03T02:21:03.127 回答
2

The reason this is not working is because you are using part of an array in a string. In order to do that, you have a few options:

  1. Use curly braces around it:

    "UPDATE Users SET day_started=1 WHERE email='{$user_data['email']}'"

  2. Concatenation:

    "UPDATE Users SET day_started=1 WHERE email='".$user_data['email']."'"

  3. Use temporary variables:

    $email = $user_data['email'];

    Then in your string:

    "UPDATE Users SET day_started=1 WHERE email='$email'"

  4. (bonus, I just learned this myself) Remove the quotes around email:

    "UPDATE Users SET day_started=1 WHERE email='$user_data[email]'"

    It actually surprised me that this works and doesn't throw a notice/warning. The following, however, does produce a notice, so be careful (it needs to be in a double quoted string or probably heredoc):

    echo $user_data[email];

    Notice: Use of undefined constant email - assumed 'email'

However the fact that you are even asking this poses some great problems. First, you should turn on error_reporting. Therefore if any error occurs, it will yell and scream at you if all goes well. Second, you should do the same for mysqli. And finally, if you're using mysqli, use prepared statements. This is precisely what they're for.

于 2013-08-03T02:36:13.720 回答
0

这是因为单引号匹配最接近它的。所以在你的代码email='$user_data['email']'中,第一个单引号'$正好匹配['email。为了避免这种情况,你可以像这样修改你的 sql 代码:

"UPDATE Users SET day_started=1 WHERE email='$user_data[email]'",this really works.

或者只是这样做:

$post_email = $user_data['email'];
"UPDATE Users SET day_started=1 WHERE email='$post_email'";
于 2013-08-03T03:27:37.817 回答