简体   繁体   中英

Else statement not executing in PHP condition

I have an if/else condition in a PHP function that sets the status of a submitted form. If the form already exists, then the form ID is stored as a session variable and that tells the function which form to update. If the form is brand new, then the form ID does not exist as a session variable and the ELSE statement should execute. The code is written to retrieve the last record in the database in the case of a new form.

//Set the form status
if(isset($_SESSION['formId']))
{
//Update the status of the EXISTING record
$setStat = $con->prepare("UPDATE table SET Status = :status WHERE
    formID = :formid");
$data = array('status'=>$status,'formid'=>$_SESSION['formId']);
$setStat -> execute($data);
}

else
{
//Update the status of the NEWLY CREATED record
$id = $con->prepare("SELECT TOP 1 formID FROM table ORDER BY formID 
    DESC");
$id->execute();
$row = $id->fetch(PDO::FETCH_ASSOC);
$formId = $row['formID'];

$setStatNew = $con->prepare("UPDATE table SET Status = :status WHERE 
    formID = :formid");
$data2 = array('status'=>$status,'formid'=>$formId);
$setStatNew -> execute($data2);
}

The function is called after the new record gets written, and I've been able to confirm that, in the case of a new form, the form id session variable is NOT set and that the query to retrieve the newest record from the DB works. I can successfully echo out the newest form ID. The ELSE statement never executes, however. When a new form is submitted, the status remains NULL. What could I be missing?

if you dont call

session_start();

session do not work!

my idea is

if($_SESSION['formId']=="submit")
{
    //your code
}
else
{
     // your code
    $_SESSION['formId']="submit";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM