2015-04-15 71 views
0

我试图实现多个if条件来检查提案的状态,并且只应执行特定代码(如果状态代码设置为1或5)。PHP IF/ELSE条件不能按预期工作

由于某种原因,我在实施这方面遇到了困难。目前代码中的逻辑是,如果提案状态不匹配1或5,则返回消息,否则执行下一个查询。当我只指定一个数字,即(1或5)时,它会正常工作。

我与若和其他条件,在这部分面临着另一个问题:

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很多这种说法。任何指导将非常感谢!

预先感谢您!

+4

表达式$ proposalstatus!= 1 || 5'并没有做到你的意思。你必须完全表达这两个条件,所以它看起来像'if($ proposalstatus!= 1 && $ proposalstatus!= 5)',这意味着将其改为'&&'使逻辑工作。 –

+0

'vardump($ proposalstatus);',你应该使用。 – Hackerman

+0

print_r($ proposalstatus)也很有用 –

回答

2

你的条件不

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 { 
} 

让别人最终如果/当$count >= 1

+0

感谢您的回复。基本上,我只是想检查计数是否为1,然后是否返回第一条消息,如果不是则执行第二部分。所以你说我应该这样做,如果条件? – user610

+0

好吧,考虑一下你的代码有第1部分(第一个if),然后是第2A部分和第2B部分。按照书面的说法,如果'$ count'确实是1,那么你会执行第1部分和第2B部分。不知道这是否是你想要的。 –

+0

我有点困惑。第1部分将检查计数== 1,如果它不是,那么第2A部分将计数<1,然后检查提议的状态,如果这一切都好,那么我只是希望它从那里继续。我觉得我自己很困惑,因为我有很多嵌套的if语句来检查各种东西 – user610

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>'; 
} 
+0

感谢您的意见。这个解决方案和@MichaelBerkowski提供的解决方案都是完美的:-) – user610

+0

欢迎您:-) –