PHP IF/ELSE条件未按预期工作
我试图实现多个if-else条件,这些条件检查提案的状态,如果状态代码设置为1或5,则只应执行特定代码 出于某种原因,我在实现这一点上遇到了困难。当前代码中的逻辑是,如果提案状态与1或5不匹配,则返回一条消息,否则执行下一个查询。当我只指定一个数字,即(1或5),它将正常工作 我在if和else条件下面临的另一个问题是这一部分:PHP IF/ELSE条件未按预期工作,php,if-statement,conditional-statements,Php,If Statement,Conditional Statements,我试图实现多个if-else条件,这些条件检查提案的状态,如果状态代码设置为1或5,则只应执行特定代码 出于某种原因,我在实现这一点上遇到了困难。当前代码中的逻辑是,如果提案状态与1或5不匹配,则返回一条消息,否则执行下一个查询。当我只指定一个数字,即(1或5),它将正常工作 我在if和else条件下面临的另一个问题是这一部分: if ($count == 1) { $feedback = '<p class="text-danger"> You have alre
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($count==1){
$feedback='您已接受申请。您不能接受或申请任何其他申请。如果这是一个错误,请直接与相关主管联系。
';
}
如果($count<1){
$status=$db\u conx->prepare(“从记录中选择状态代码,其中学生记录\u id=:用户记录\u id和提案\u id=:提案”);
$status->bindParam(':user\u record\u id',$user\u record\u id,PDO::PARAM\u STR);
$status->bindParam(':proposal',$proposal,PDO::PARAM_STR);
$status->execute();
$proposalstatus=$status->fetchColumn();
如果($proposalstatus!=1)
{
//echo$proposalstatus;
$feedback='提案未处于可接受状态
;
}
}
否则{
当我单独运行每个部分时,这是可行的,但当我尝试将其放在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>';
}
}
试试看
{
$db_conx=new PDO(“mysql:host=$mysql_hostname;dbname=$mysql_dbname”、$mysql_username、$mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
$username=$\会话['username'];
$sql=$db\u conx->prepare(“从登录详细信息中选择用户名、用户记录\u id
其中username=:username“;
$sql->bindParam(':username',$username,PDO::PARAM_STR);
$sql->execute();
$user\u record\u id=$sql->fetchColumn(1);
$PROVISSION=$\u POST['PROVISSION\u id'];
$acceptCheck=$db\u conx->prepare(“从记录中选择*,其中student\u record\u id=:user\u record\u id和status\u code=3”);
$acceptCheck->bindParam(':user\u record\u id',$user\u record\u id,PDO::PARAM\u STR);
$acceptCheck->execute();
$count=$acceptCheck->rowCount();
如果($count==1){
$feedback='您已接受申请。您不能接受或申请任何其他申请。如果这是一个错误,请直接与相关主管联系。
';
}
如果($count<1){
$status=$db\u conx->prepare(“从记录中选择状态代码,其中学生记录\u id=:用户记录\u id和提案\u id=:提案”);
$status->bindParam(':user\u record\u id',$user\u record\u id,PDO::PARAM\u STR);
$status->bindParam(':proposal',$proposal,PDO::PARAM_STR);
$status->execute();
$proposalstatus=$status->fetchColumn();
if($proposalstatus!=1 | | 5)//状态必须为“批准”代码1或“保留”代码5
{
//echo$proposalstatus;
$feedback='提案未处于可接受状态
;
}
}
否则{
//接受提案后,将所有申请记录更新为“不可用”
$updateOtherRecords=$db\u conx->prepare(“更新记录集状态\u代码=8,上次更新=现在()
其中提案_id=:提案“);
$updateOtherRecords->bindParam(“:proposal”,$proposal,PDO::PARAM_STR);
$updateOtherRecords->execute();
//将相关用户的其他应用程序A更新为“已拒绝”
$updateUserRecord=$db\u conx->prepare(“更新记录集状态\u代码=7,上次更新=现在()
其中,student_record_id=:user_record_id”);
$updateUserRecord->bindParam(':user\u record\u id',$user\u record\u id,PDO::PARAM\u STR);
$updateUserRecord->execute();
//更新相关提案并将其分配给用户
$update=$db\u conx->prepare(“更新记录集状态\u代码=3,上次更新=现在()
其中,提案_id=:提案和学生_记录_id=:用户_记录_id”);
$update->bindParam(':user\u record\u id',$user\u record\u id,PDO::PARAM\u STR);
$update->bindParam(“:proposal”,$proposal,PDO::PARAM_STR);
$update->execute();
$feedback='提案已被成功接受
;
}
}
我真的需要知道我如何排序,因为我会在这个声明中大量使用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
}
如果($count<1){
一些东西
}
如果($count==1){
...
}否则
…此代码将在$count*不*等于1时执行,
这包括小于1时,例如此处的“<1”为真
}
也许你想要
if ($count == 1) {
} else if ($count < 1) {
} else {
}
if($count==1){
}否则如果($count<1){
}否则{
}
因此,只有当您的条件不相互排斥时,才会运行最终的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
}
如果($count<1){
一些东西
}
如果($count==1){
...
}否则
... 当$count*不*等于1时,将执行此代码,
这包括小于1时,例如此处的“<1”为真
}
也许你想要
if ($count == 1) {
} else if ($count < 1) {
} else {
}
if($count==1){
}否则如果($count<1){
}否则{
}
因此,只有当您的条件不相互排斥时,才会运行最终的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
}
如果($count<1){
一些东西
}
如果($count==1){
...
}否则
... 当$count*不*等于1时,将执行此代码,
这包括小于1时,例如此处的“<1”为真
}
<