0

我不知道为什么会收到此错误,但它一直说我有一个未定义的索引:AudioFile on line 35 in line 35 in below code:

<?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>
4

1 回答 1

0

未定义的索引意味着 $_SESSION 中不存在键“lastAudioID”。您可以通过以下方式检查:

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

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

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

于 2012-10-23T16:24:05.827 回答