2014-11-14 68 views
1

我試圖插入數據到MySQL表使用mysqli但這段代碼不工作,我沒有得到任何錯誤打印出來。MySQLi沒有插入表

我可能會錯過什麼?

<?php 
// if the form was submitted 
if($_POST){ 

    // sql query 
    $sql2 = "INSERT INTO 
       t_incident_persons (IncidentID, PersonID, KeywordID, description) 
      VALUES 
       (?, ?, ?, ?)"; 

    // if the statement was prepared 
    if($stmt2 = $mysqli->prepare($sql2)){ 

     //bind the values, 
     $stmt2->bind_param(
      "iiis", 
      $_POST['IncidentID'], 
      $_POST['PersonID'], 
      $_POST['KeywordID'], 
      $_POST['description'] 
     ); 

     // execute the insert query 
     if($stmt2->execute()){ 
    header('Location: details.php'); 
    exit; 
      $stmt2->close(); 
     }else{ 
      die("Unable to add an incident."); 
     } 

    }else{ 
     die("Unable to prepare statement."); 
    } 

    // close the database 
    $mysqli->close(); 
} 

?> 

回答

0

我相信這是自動提交行爲。嘗試在重定向之前關閉之前關閉。

... 
if($stmt2->execute()){ 
    $stmt2->close(); 
    header('Location: details.php'); 
    exit;    
}else{ 
    die("Unable to add an incident."); 
} 
+0

不知道我理解你,但沒有你給出的代碼塊我得到一個錯誤:意外的文件結束。 – 2014-11-14 14:12:58