簡體   English   中英

PHP IF / ELSE條件無法按預期工作

[英]PHP IF/ELSE condition not working as expected

我正在嘗試實施多個條件,這些條件將檢查提案的狀態,並且僅在狀態代碼設置為1或5時才應執行特定的代碼。

由於某種原因,我在實施此程序時遇到了困難。 當前代碼中的邏輯是,如果投標狀態與1或5不匹配,則返回一條消息,否則執行下一個查詢。 當我僅指定一個數字(即1或5)時,它將正常工作。

我和if和else條件面臨的另一個問題是這部分:

if ($count == 1) {

        $feedback = '<p class="text-danger"> You have already accepted an application. You cannot accept or apply for any others. If this is a mistake then please contact the supervisor concerned directly.</p>'; 
    }

    if ($count < 1) {

        $status = $db_conx->prepare ("SELECT status_code FROM record WHERE student_record_id = :user_record_id AND proposal_id = :proposal");

        $status->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $status->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $status->execute();

        $proposalstatus = $status->fetchColumn();

        if($proposalstatus != 1)
        {
                //echo $proposalstatus;
            $feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>';
        }
    }

    else {

當我分別運行每個部分時,這是可行的,但是當我嘗試將其放到if語句中時,它將失敗並且根本不檢查這些條件,而只是完成了更新數據庫並顯示成功消息的任務。

完整的代碼在這里:

try
    {
     $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

     $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     $username = $_SESSION['username'];

     $sql = $db_conx->prepare("SELECT username, user_record_id FROM login_details
        WHERE username = :username");

     $sql->bindParam(':username', $username, PDO::PARAM_STR);

     $sql->execute();
     $user_record_id = $sql->fetchColumn(1);

     $proposal = $_POST['proposal_id'];

     $acceptCheck = $db_conx->prepare ("SELECT * FROM record WHERE student_record_id = :user_record_id AND status_code = 3");
     $acceptCheck->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
     $acceptCheck->execute();

     $count = $acceptCheck->rowCount();

     if ($count == 1) {

        $feedback = '<p class="text-danger"> You have already accepted an application. You cannot accept or apply for any others. If this is a mistake then please contact the supervisor concerned directly.</p>'; 
    }

    if ($count < 1) {

        $status = $db_conx->prepare ("SELECT status_code FROM record WHERE student_record_id = :user_record_id AND proposal_id = :proposal");

        $status->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $status->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $status->execute();

        $proposalstatus = $status->fetchColumn();

        if($proposalstatus != 1 || 5) //status must be either 'Approved' code 1 or 'Held' code 5
        {
                //echo $proposalstatus;
            $feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>';
        }
    }

    else {

                //Update all application records to 'Not available' when a proposal has been accepted

        $updateOtherRecords = $db_conx->prepare("UPDATE record SET status_code = 8, last_updated = now()
            WHERE proposal_id = :proposal");
        $updateOtherRecords->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $updateOtherRecords->execute();

                //Update other applicationa for the user concerned to 'Rejected'

        $updateUserRecord = $db_conx->prepare("UPDATE record SET status_code = 7, last_updated = now()
            WHERE student_record_id = :user_record_id");
        $updateUserRecord->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $updateUserRecord->execute();

                //Update the proposal concerned and assign it to the user

        $update = $db_conx->prepare("UPDATE record SET status_code = 3, last_updated = now()
            WHERE proposal_id = :proposal AND student_record_id = :user_record_id");
        $update->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $update->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $update->execute();

        $feedback = '<p class="text-success"> The proposal has been successfully accepted <span class="glyphicon glyphicon-ok"/></p>';
    }
} 

我真的需要知道如何對它進行排序,因為在此語句中我將大量使用if和else。 任何指導將不勝感激!

先感謝您!

您的條件並不互相排斥

if ($count < 1) { 
  some stuff
}

if ($count == 1) {
 ...
} else 
 ... this code will execute when $count is *NOT* equal to 1,
  which includes when it's LESS than 1, e.g. "< 1" is true here
}

也許你想要

if ($count == 1) {
} else if ($count < 1) {
} else {
}

這樣最終的else僅在$count >= 1時運行

用提案狀態1或5替換您的條件

if(!($proposalstatus == 1 || $proposalstatus == 5)) {
$feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM