简体   繁体   English

更新:如果条目不存在,则没有错误

[英]update: no error, if entry doesnt exist

I'm just wondering: if an entry doesn't exist, "UPDATE" also makes an "update"? 我只是在想:如果一个条目不存在,“ UPDATE”是否也会产生一个“ update”? I use following code and the if-statement always has "success", especially when the email can't be found by "WHERE". 我使用以下代码,并且if语句始终具有“成功”,尤其是当“ WHERE”无法找到电子邮件时。 Do I have to check, if the email exists in the table? 我是否需要检查表中是否存在电子邮件?

$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");

Thanks for any suggestions. 感谢您的任何建议。

that's because you redirect either if the match exists or not 那是因为如果存在匹配项,您将重定向

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

also remove that if before your query, so final code will look like if在查询之前也将其删除, 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");
}

You need to retrieve the number of rows that were affected by the previous operation. 您需要检索受上一操作影响的行数。 This is provided by affected_rows . 这由affected_rows提供。

if ($mysqli->affected_rows == 1)
{
   echo 'A record was deleted';
}
$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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM