如何在$ _SESSION的代码中使用isset()命令

时间:2012-10-23 00:07:08

标签: php pdo mysqli

我有一个基本的问题,询问如何使用isset();命令以及如何在下面的示例中编写它。我试图将值插入查询,然后检索插入的最后一个ImageId并将其插入另一个表

    session_start();


         $imagesql = "INSERT INTO Image (ImageFile) 
        VALUES (?)";

                if (!$insert = $mysqli->prepare($imagesql)) {
          // Handle errors with prepare operation here
        }

        //Dont pass data directly to bind_param store it in a variable
    $insert->bind_param("s",$img);

    //Assign the variable
    $img = 'ImageFiles/'.$_FILES['fileImage']['name'];

     $insert->execute();

            if ($insert->errno) {
              // Handle query error here
            }

            $insert->close();

            $lastImageID = $mysqli->insert_id;     

        $_SESSION['lastImageID'] = $lastImageID; 

$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)  
    VALUES (?, ?, ?)"; 

     if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
      // Handle errors with prepare operation here 
       echo "Prepare statement err imagequestion"; 
    } 

$qnum = (int)$_POST['numimage'];

$insertimagequestion->bind_param("iii",$lastImageID, $sessionid, $qnum); 

    $insertimagequestion->execute(); 

                if ($insertimagequestion->errno) { 
          // Handle query error here 
        } 

        $insertimagequestion->close(); 

2 个答案:

答案 0 :(得分:0)

不完全确定你的要求,但疯狂地猜测:

session_start();

...... //PDO code and commands

   $lastImageID = $mysqli->insert_id; 
   $_SESSION['lastImageID'] = $lastImageID; 

   if (isset($_SESSION['lastImageID'])) {
      // do this
   } else {
      // do that
   } 

答案 1 :(得分:0)

如果您正在阅读可能未定义的变量,则只需要调用isset

在这种情况下,您正在写信$_SESSION['lastImageID'],因此您无需检查。

需要isset的示例:

if (isset($_SESSION['lastImageID']))
{
    $lastImageID = $_SESSION['lastImageID'];
}

else
{
    $lastImageID = get_this_from_db_or_something();
}