MySQLi没有插入表

时间:2014-11-14 13:28:44

标签: mysqli

我正在尝试使用mysqli将数据插入到mysql表中,但是这段代码无效,我没有打印出任何错误。

我可能会错过什么?

<?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();
}

?>

1 个答案:

答案 0 :(得分:0)

我相信它的自动提交行为。在重定向之前尝试在连接之前关闭。

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