user610
user610

Reputation: 353

PHP IF/ELSE condition not working as expected

I'm attempting to implement multiple if else conditions which check the status of a proposal and should only execute a specific code if the status code is set to 1 or 5.

For some reason I'm having difficulties with implementing this. Currently the logic in the code is if the proposal status does not match 1 or 5 then returns a message otherwise execute the next query. When I specify just the one number i.e. (1 or 5) it will work fine.

Another problem I am facing with if and else conditions is in this part:

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 {

This works when I run each part separately but when I try and put it together in an if statement it fails and doesn't check for these conditions at all and just completes the task which updates the database and displays a success message.

The full code is here:

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

I really need to know how I can sort this because I'll be using if and else a lot in this statement. Any guidance would be much appreciated!

Thank you in advance!

Upvotes: 0

Views: 616

Answers (2)

Ari Djemana
Ari Djemana

Reputation: 1249

replace your condition with proposal status 1 or 5

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

Upvotes: 1

Marc B
Marc B

Reputation: 360672

Your conditions aren't mutually exclusive

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
}

Perhaps you want

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

So that the final else will only run if/when $count >= 1

Upvotes: 2

Related Questions