0

我只是想知道:如果一个条目不存在,“更新”也会进行“更新”吗?我使用以下代码,并且 if 语句始终具有“成功”,尤其是当“WHERE”找不到电子邮件时。我是否必须检查电子邮件是否存在于表格中?

$email=...;
if ($update_stmt = $mysqli->prepare("UPDATE members SET password = ?, salt = ? WHERE email='".$email."'"))
         {
         $update_stmt->bind_param('ss', $password, $random_salt);

         $update_stmt->execute()
         header("Location: ...?success=1");
         }
         else
            header("Location: ...?error=1");

感谢您的任何建议。

4

3 回答 3

1

那是因为如果匹配存在或不存在,您会重定向

$update_stmt->execute()
if($update_stmt->affected_rows != 0)
{
    header("Location: ...?success=1");
}
else
{   
    header("Location: ...?error=1");
}

还要在查询之前将其删除if,因此最终代码将如下所示

$update_stmt = $mysqli->prepare("UPDATE members SET password = ?, salt = ? WHERE email='".$email."'"))
$update_stmt->bind_param('ss', $password, $random_salt);
$update_stmt->execute()
if($update_stmt->affected_rows != 0)
{
    header("Location: ...?success=1");
}
else
{   
    header("Location: ...?error=1");
}
于 2013-04-22T07:01:04.413 回答
0
$email=...;
$mysqli->prepare("UPDATE members SET password = ?, salt = ? WHERE email='".$email."'"))
$update_stmt->bind_param('ss', $password, $random_salt);
$update_stmt->execute();

if ($update_stmt->affected_rows > 0){
   header("Location: ...?success=1");
         }
else
   header("Location: ...?error=1");
于 2013-04-22T07:09:48.180 回答
0

您需要检索受先前操作影响的行数。这是由 提供的affected_rows

if ($mysqli->affected_rows == 1)
{
   echo 'A record was deleted';
}
于 2013-04-22T07:01:40.103 回答