簡體   English   中英

MySQLI INSERT 查詢執行兩次

[英]MySQLI INSERT query is executed twice

我有以下代碼:

// open the database connection
require_once('php/db/connect.php');
// set a log file
$logFile = 'log/php.log';
// set the error and the doCommit to false
$error = false;
$doCommit = false;
// build the query
$query = "SELECT name,gender,email,country FROM tmp_backer WHERE backerid = ?";
// prepare the query
if ($stmt = $mysqli->prepare($query)) {
    // bind the parameters
    $id = $postArray['backerid'];
    $stmt->bind_param('s',  $id);
    // execute the query
    if ($stmt->execute()) {
        // the query is executed!
        $stmt->store_result();
        $tmpArray = array();
        $stmt->store_result();
        $stmt->bind_result( $name,$gender,$email,$country);
        $stmt->fetch();
        // clear the parameters
        $query = '';
        // copy everything to the table 'backer'
        // build the query
        $query = "INSERT INTO backer (backerid,name,gender,email,country) VALUES (?,?,?,?,?)";
        // prepare the query
        if ($stmt = $mysqli->prepare($query)) {
            // bind the parameters
            $stmt->bind_param(
                'ssiss',        
                $id,
                $name,
                $gender,
                $email,
                $country
            );
            if ($stmt->execute()) {
                // the query is executed!
                // set doCommit to true
                $doCommit = true;
            } else {
                // the query could not be executed
                // log this into the log file and send the visitor back to the become a backer page
                error_log(date('Y-m-d H:i:s').' | query could not be executed (INSERT query) '.$stmt->error.PHP_EOL,3,$logFile);
                $error = true;
            };
        } else {
            // the query is invalid
            // log this into the log file and send the visitor back to the become a backer page
            error_log(date('Y-m-d H:i:s').' | query is invalid (INSERT query) '.$stmt->error.PHP_EOL,3,$logFile);
            $error = true;
        };
    } else {
        // the query could not be executed
        // log this into the log file and send the visitor back to the become a backer page
        error_log(date('Y-m-d H:i:s').' | query could not be executed (SELECT query) '.$stmt->error.PHP_EOL,3,$logFile);
        $error = true;
    };
} else {
    // the query is invalid
    // log this into the log file and send the visitor back to the become a backer page
    error_log(date('Y-m-d H:i:s').' | query is invalid (SELECT query) '.$stmt->error.PHP_EOL,3,$logFile);
    $error = true;
};
// check if there where any errors whilst preparing/executing
// the query
if ($error == true) {
    // there were errors
    $mysqli->rollback();
    exit();
} else {
    // there were no errors
    // doCommit was set to true
    if ($doCommit == true) {
        $mysqli->commit();
    };
};
// close the db connection
require_once('php/db/close.php');

第二個查詢執行兩次,因為我在包含以下文本的日志文件中收到錯誤消息:

2015-09-14 21:14:12 | 無法執行查詢(INSERT 查詢)鍵“PRIMARY”的重復條目“qzdkzkdjzdk”

backerID是生成的隨機鍵,是兩個表中的主鍵(字段: backerid )。 查詢WORKS,所以數據從表tmp_backer復制到backer,但是問題是頁面沒有顯示(因為第二次執行INSERT查詢,遇到錯誤所以轉到了它所在的部分如: $mysqli->rollback(); exit();

誰能幫我這個?

刪除這一行:

if ($doCommit == true) {
    $mysqli->commit();
};

並將其替換為:

$mysqli->commit();

因為if($error == true)已被調用,所以在“else”之后沒有其他東西會更進一步,但是如果它是“false”並且因為它是“false” $doCommit必須是“true”,所以,自動“提交它“而不是叫它回來。 問題是,您讓$doCommit激活兩次。 首先,通過常規調用,如果結果為真,則在您要求“它”時再次調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM