如果我的情况下,IF语句不起作用

时间:2018-11-08 03:37:04

标签: php

if (isset($_POST["submit2"])) {
    $uid = $_SESSION["uid"];
    $remove_id = $_POST["remove_id"];
    $sql = "DELETE FROM orders WHERE product_id = '$remove_id' AND user_id = '$uid'";
    $run_query = mysqli_query($link,$sql);
    $payment = $_POST["p_status"];
    $method = "Paypal";
    if ($payment == $method) {
        echo "Please contact the company";
    }
    else if ($run_query) {                                      
        echo "<meta http-equiv='refresh' content='0'>";
    }
}

我的if语句不起作用。它总是继续run_query.please帮助我,谢谢。 p_status是“ Paypal”

2 个答案:

答案 0 :(得分:0)

您的问题是,由于查询中没有错误,因此mysqli_query每次运行查询都会返回true,即使没有要删除的行。您需要做的是检查是否使用mysqli_affected_rows删除了任何行:

if($payment == $method) {
    echo "Please contact the company";
} 
else if (mysqli_affected_rows($link) > 0) {                                       
    echo "<meta http-equiv='refresh' content='0'>";
}

答案 1 :(得分:0)

可能是因为您甚至在进行条件处理之前就已经运行了SQL。正如我所注意到的

  

$ run_query = mysqli_query($ link,$ sql)//在执行if语句并将其结果分配给$ run_query变量之前已经执行了此操作。

尝试用此代码修改代码

if (isset($_POST["submit2"])) {
    $uid = $_SESSION["uid"];
    $remove_id = $_POST["remove_id"];
    $sql = "DELETE FROM orders WHERE product_id = '$remove_id' AND user_id = '$uid'";
    $payment = $_POST["p_status"];
    $method = "Paypal";
    if ( $payment == $method ) { // It will check first if condition is true
        echo "Please contact the company";
    } else if ( mysqli_query($link,$sql) ) { // It will be executed it the condition is false.                                  
        echo "<meta http-equiv='refresh' content='0'>";
    }
}

希望这会有所帮助:)