PHP IF/ELSE 条件未按预期工作
PHP IF/ELSE condition not working as expected
我正在尝试实施多个 if else 条件来检查提案的状态,并且应该仅在状态代码设置为 1 或 5 时执行特定代码。
出于某种原因,我在实施时遇到了困难。目前代码中的逻辑是,如果提案状态不匹配 1 或 5,则 returns 一条消息,否则执行下一个查询。当我只指定一个数字时,即(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只会运行if/when$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>';
}
我正在尝试实施多个 if else 条件来检查提案的状态,并且应该仅在状态代码设置为 1 或 5 时执行特定代码。
出于某种原因,我在实施时遇到了困难。目前代码中的逻辑是,如果提案状态不匹配 1 或 5,则 returns 一条消息,否则执行下一个查询。当我只指定一个数字时,即(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只会运行if/when$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>';
}