即使失败,PHP也始终报告SQL成功

时间:2017-09-21 01:45:53

标签: php mysql mysqli

这是一个简单的,也许我有一个长脑屁或其他什么,但发生什么是形式用于设置记录只有当名称和他们的cpukey匹配然后它将继续,因为这是一个没有登录的公共页面,人们在不知道2组信息的情况下改变其他人的事情会很烦人。所以这里的问题是,函数本身实际上是完美无缺的,即使函数失败(由于一行或多行不匹配),生成的消息SUCCESS或FAILURE也会产生SUCCESS

以下是使用的代码。

if(isset($_POST['upduserNotify'])){
    $ccurrentname = mysqli_real_escape_string($con, $_POST['ccnameVal']); 
    $cclientnotify = mysqli_real_escape_string($con, $_POST['cnotifyVal']);
    $cclientcpuk = mysqli_real_escape_string($con, $_POST['ccpukeyVal']);

    $changenot = "UPDATE clients SET notify = '$cclientnotify' WHERE cpukey = '$cclientcpuk' AND name = '".$_POST['ccnameVal']."'";

    if (mysqli_query($con, $changenot)) {
        echo'<div align ="center" style="color:#000000">Your Custom notification was updated successfully</div><br />';
    } else {
        echo'<div align ="center">You entered an incorrect CPUKEY/Name or used an invalid character</div><br />';
    }
}

2 个答案:

答案 0 :(得分:1)

运行的UPDATE查询,但找不到要更新的行,仍然是成功的 - 它在没有遇到任何错误的情况下运行完成,因此mysqli_query将返回TRUEper the docs。 (如果是SELECT种查询,则会返回mysqli_result个对象。)

如果您希望在找不到要更新的行时执行其他操作,则您需要查看the number of affected rows并采取相应措施。

答案 1 :(得分:-1)

        if(isset($_POST['upduserNotify'])){
            $ccurrentname = mysqli_real_escape_string($con, $_POST['ccnameVal']); 
            $cclientnotify = mysqli_real_escape_string($con, $_POST['cnotifyVal']);
            $cclientcpuk = mysqli_real_escape_string($con, $_POST['ccpukeyVal']);

        $changenot = "UPDATE `clients` SET `notify` = '$cclientnotify' WHERE `cpukey` = '$cclientcpuk' AND `name` = '$ccurrentname'";

                if (mysqli_query($con, $changenot) && mysqli_affected_rows($con) == 1 )
                {
                    echo'<div align ="center" style="color:#000000">Your Custom Xnotify was updated successfully</div><br />';
                } 
                else if (mysqli_query($con, $changenot) && mysqli_affected_rows($con) == 0 )
                {
                    echo'<div align ="center">You entered an incorrect CPUKEY/Name or used an invalid character</div><br />';
                }
            }