1

我有一个基本问题,询问如何使用 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(); 
4

2 回答 2

0

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

session_start();

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

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

   if (isset($_SESSION['lastImageID'])) {
      // do this
   } else {
      // do that
   } 
于 2012-10-23T00:10:41.730 回答
0

isset仅当您正在读取可能未定义的变量时才需要调用。

在这种情况下,你正在写信给$_SESSION['lastImageID'],所以你不需要检查。

需要的示例isset

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

else
{
    $lastImageID = get_this_from_db_or_something();
}
于 2012-10-23T00:46:47.253 回答