简体   繁体   English

其他语句未在PHP条件下执行

[英]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. 我在PHP函数中有一个if / else条件,该条件设置提交表单的状态。 If the form already exists, then the form ID is stored as a session variable and that tells the function which form to update. 如果表单已经存在,则将表单ID存储为会话变量,并告诉函数要更新的表单。 If the form is brand new, then the form ID does not exist as a session variable and the ELSE statement should execute. 如果表单是全新的,则表单ID不作为会话变量存在,并且应执行ELSE语句。 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. 写入新记录后将调用该函数,并且我已经能够确认,对于新表单,未设置表单id会话变量,以及从数据库检索最新记录的查询是否有效。 I can successfully echo out the newest form ID. 我可以成功地回显最新的表单ID。 The ELSE statement never executes, however. 但是,ELSE语句从不执行。 When a new form is submitted, the status remains NULL. 提交新表单时,状态保持为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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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