更新行但在更新之前,输入框应该符合记录

时间:2016-09-25 11:43:33

标签: php mysql

即使找不到'success',以下内容始终会回显mname。如果找不到,则应显示'failed'。当我输入未找到的'success'时,为什么显示mname

<?php 
$conn = mysqli_connect("localhost","root","","lpdb"); 

//update 
if (isset($_POST['forgot'])){ 
    $password = 'password'; 
    $mname = $_POST['mname']; 
    $query = "UPDATE logindb SET password ='$mname' WHERE mname = '$mname'"; 
    $result = mysqli_query($conn, $query); 
    if ($result) { 
        echo 'success'; 
    } else { 
        echo 'failed'; 
    } 
}
?>

2 个答案:

答案 0 :(得分:0)

来自the documentation

  

失败时返回FALSE。对于成功的SELECTSHOWDESCRIBEEXPLAIN个查询mysqli_query()将返回mysqli_result个对象。对于其他成功的查询,mysqli_query()将返回TRUE

不更新任何内容的更新不是失败,因此结果为TRUE。使用mysqli_affected_rows查看UPDATE

更新了多少行
<?php 
$conn = mysqli_connect("localhost","root","","lpdb"); 

//update 
if (isset($_POST['forgot'])){ 
    $password = 'password'; 
    $mname = $_POST['mname']; 
    $query = "UPDATE logindb SET password ='$mname' WHERE mname = '$mname'"; 
    mysqli_query($conn, $query);           // ***
    $result = mysqli_affected_rows($conn); // ***
    if ($result > 0) {                     // *** Or perhaps == 1, up to you
        echo 'success'; 
    } else { 
        echo 'failed'; 
    } 
}
?>

答案 1 :(得分:0)

首先检查mname

<?php

//check if user submitted first
if (isset($_POST['forgot'])){ 
$password = 'password'; 
$mname = $_POST['mname']; 

$conn = mysqli_connect("localhost","root","","lpdb"); 
//update 
$query = "UPDATE logindb SET password ='$mname' WHERE mname = '$mname'"; 
$result = mysqli_query($conn, $query); 
if($result){
    echo 'success'; 
} else { 
    echo 'failed to update for some reason';
//or maybe echo the db err
echo mysqli_error(); 
} 
?