为什么我收到此$ _SESSION变量的未定义索引错误

时间:2012-10-23 16:21:17

标签: php pdo mysqli

我不知道为什么我会收到此错误,但它一直声明我有一个未定义的索引:以下代码中第35行的AudioFile:

<?php

 ini_set('display_errors',1); 


session_start();

?>

<body>

<?php

 // connect to the database
 include('connect.php');

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

  unlink("AudioFiles/" . $_SESSION['AudioFile']);  

$delete = $mysqli->prepare('DELETE FROM Audio WHERE AudioId = ?'); 
$delete->bind_param("i",$_SESSION['lastAudioID']); 
$delete->execute(); 

$deleteaud = $mysqli->prepare('DELETE FROM Audio_Question WHERE AudioId = ?'); 
$deleteaud->bind_param("i",$_SESSION['lastAudioID']); 
$deleteaud->execute(); 


?>


</body>
</html>

我不太明白为什么我收到这个索引错误。有谁知道为什么我会收到这个错误?

我在audioupload.php页面中定义了$ _SESSION变量,该变量位于

之下
        <?php

ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_divisor', '1');
ini_set('session.gc_probability', '1');
ini_set('session.cookie_lifetime', '0');
require_once 'init.php'; 


session_start();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<?php
 ini_set('display_errors',1); 
 error_reporting(E_ALL);

 // connect to the database
 include('connect.php');

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

$result = 0;

    if ((($_FILES["fileAudio"]["type"] == "audio/wav")
|| ($_FILES["fileAudio"]["type"] == "audio/wma")
|| ($_FILES["fileAudio"]["type"] == "audio/mp3")
|| ($_FILES["fileAudio"]["type"] == "audio/m3u")
|| ($_FILES["fileAudio"]["type"] == "audio/iff")
|| ($_FILES["fileAudio"]["type"] == "audio/mid")
|| ($_FILES["fileAudio"]["type"] == "audio/m4a")
|| ($_FILES["fileAudio"]["type"] == "audio/mpa")
|| ($_FILES["fileAudio"]["type"] == "audio/aif")
|| ($_FILES["fileAudio"]["type"] == "audio/wa"))
&& ($_FILES['fileAudio']['size'] > 0))
  {

if( file_exists("AudioFiles/".$_FILES['fileAudio']['name'])) {
    $parts = explode(".",$_FILES['fileAudio']['name']);
    $ext = array_pop($parts);
    $base = implode(".",$parts);
    $n = 2;

    while( file_exists("AudioFiles/".$base."_".$n.".".$ext)) $n++;
    $_FILES['fileAudio']['name'] = $base."_".$n.".".$ext;

    move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
    "AudioFiles/" . $_FILES["fileAudio"]["name"]);
    $result = 1; 

}
    else
      {
      move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
      "AudioFiles/" . $_FILES["fileAudio"]["name"]);
      $result = 1;

  }

    $audiosql = "INSERT INTO Audio (AudioFile) 
    VALUES (?)";

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

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

//Assign the variable
$aud = 'AudioFiles/'.$_FILES['fileAudio']['name'];

 $insert->execute();

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

        $insert->close();

 $lastAudioID = $mysqli->insert_id;   

$_SESSION['lastAudioID'] = $lastAudioID; 
$_SESSION['AudioFile'] = $_FILES["fileAudio"]["name"]; 

 $sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');         

$sessionquery = "SELECT SessionId FROM Session WHERE (SessionName = ?)";

    if (!$sessionstmt = $mysqli->prepare($sessionquery)) {
  // Handle errors with prepare operation here
  echo __LINE__.': '.$mysqli->error;
}

// Bind parameter for statement
$sessionstmt->bind_param("s", $sessid);

// Execute the statement
$sessionstmt->execute();

            if ($sessionstmt->errno) 
            {
                // Handle query error here
                echo __LINE__.': '.$sessionstmt->error;
                break 1;
            }

// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$sessionstmt->bind_result($sessionid);

// This populates $optionid
$sessionstmt->fetch();

    $sessionstmt->close();      

 $audioquestionsql = "INSERT INTO Audio_Question (AudioId, SessionId, QuestionId)  
    VALUES (?, ?, ?)"; 

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

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

$insertaudioquestion->bind_param("iii",$lastAudioID, $sessionid, $qnum); 

    $insertaudioquestion->execute(); 

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

        $insertaudioquestion->close(); 

      }

?>

  <script language="javascript" type="text/javascript">window.top.stopAudioUpload(<?php echo $result; ?>, '<?php echo $_FILES['fileAudio']['name'] ?>');</script>  

</body>
</html>

1 个答案:

答案 0 :(得分:0)

未定义索引表示密钥&#39; lastAudioID&#39;在$ _SESSION中不存在。您可以通过以下方式查看:

if(array_key_exists('lastAudioID', $_SESSION)) {
  // Key exists
}

似乎您未在代码中设置密钥(或未正确)。

编辑: 可能是因为您在输出已经存在的内容后包含了您的文件。尝试在输出任何内容之前包含该文件。我很确定这就是它。