即使找不到'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';
}
}
?>
答案 0 :(得分:0)
失败时返回
FALSE
。对于成功的SELECT
,SHOW
,DESCRIBE
或EXPLAIN
个查询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();
}
?