2017-01-21 61 views
0

我有以下使用PHP函數的一個問題:多個PHP mysqli的預處理語句(INSERT,UPDATE,SELECT)怪異的行爲

function saveCommentOnDB($arg_textComment, $arg_score, $arg_userEmail) 
{ 
    $result_tmp = null; 

    $this->conn->autocommit(false); 

    echo "saving\n"; 
    echo "text comment: \n";  
    var_dump($arg_textComment); // OKAY 
    echo "comment score: \n"; 
    var_dump($arg_score);  // OKAY 
    echo "user mail: \n"; 
    var_dump($arg_userEmail); // OKAY 

    try { 

     //[tag1] $query_1 = "INSERT INTO commenti (userFirstname, userEmail, textComment, score, feedback) VALUES ((SELECT firstname FROM utente u WHERE u.userEmail = '[email protected]') ,'[email protected]', 'This is an example comment.', 5, 0);"; 
     $query_1 = "INSERT INTO commenti (userFirstname, userEmail, textComment, score, feedback) VALUES ((SELECT firstname FROM utente u WHERE u.userEmail = ?) ,?,?, ?, 0);"; 
     $query_2 = "UPDATE utente SET commentID=(SELECT c.commentID FROM commenti c WHERE c.userEmail = ?) WHERE userEmail = ?;"; 
     $query_3 = "SELECT commentID, textComment FROM commenti WHERE userEmail = ?;"; 

     $stmt1 = $this->conn->prepare($query_1); 
     $stmt2 = $this->conn->prepare($query_2); 
     $stmt3 = $this->conn->prepare($query_3); 

     $stmt1->bind_param("sssd", $arg_userEmail, $arg_userEmail, $arg_textComment, $arg_score); 
     $stmt2->bind_param("ss", $arg_userEmail, $arg_userEmail); 
     $stmt3->bind_param("s", $arg_userEmail); 

     $stmt1->execute(); 
     $stmt2->execute(); 
     $stmt3->execute(); 

     $stmt3->bind_result($col1, $col2); 
     $stmt3->fetch(); 

     echo "result:\n"; 
     var_dump($col1); // OKAY 
     var_dump($col2); // OKAY 

     $result_tmp = array(
      'commentID' => $col1, 
      'textComment' => $col2 
     ); 

     $this->conn->commit(); 
    } catch (Exception $e) { 
     $this->conn->rollback(); 
    } 

    return $result_tmp; 
} 

請忽略echovar_dump,我把它們僅用於調試。

問題是在這個函數中這三個準備好的語句似乎工作不正確。特別是$stmt1$stmt3的結果是正確的(好像$stmt1$stmt2被正確執行),但我在我的數據庫中沒有看到任何東西。換句話說:這些語句在執行過程中「暫時」正常工作,但在MyPHP Admin中,表commenti上沒有任何內容。

例如,我們假設在DB有這樣的:

enter image description here

現在我啓動功能具有下列參數:

  • $arg_textComment = 'This is an example comment'
  • $arg_score = '5'

,我們有我的瀏覽器控制檯上:

enter image description here

即:在commentID(28)是正確的,註釋文本(commentcomment)被 「保存」,然後我重新檢查DB但我還有這個:

enter image description here

var_dump($stmt1)後執行是:

stmt1: 
object(mysqli_stmt)#4 (10) { 
    ["affected_rows"]=> 
    int(1) 
    ["insert_id"]=> 
    int(41) 
    ["num_rows"]=> 
    int(0) 
    ["param_count"]=> 
    int(4) 
    ["field_count"]=> 
    int(0) 
    ["errno"]=> 
    int(0) 
    ["error"]=> 
    string(0) "" 
    ["error_list"]=> 
    array(0) { 
    } 
    ["sqlstate"]=> 
    string(5) "00000" 
    ["id"]=> 
    int(4) 
} 

var_dump似乎沒問題,但數據庫沒有。

所以我試圖通過這個來執行查詢「手動」(將只執行代碼到綠色粗線框):

enter image description here

,我有我的預期:

sql> INSERT INTO commenti (userFirstname, userEmail, textComment, score, feedback) VALUES ((SELECT firstname FROM utente u WHERE u.userEmail = '[email protected]') ,'[email protected]', 'commentcomment', '5', 0) [2017-01-21 17:38:28] 1 row affected in 11ms 

enter image description here

記住score值是DB店作爲float

$stmt1的SQL查詢與我手動插入的(INSERT INTO...通過PHPStorm)相同。

爲什麼第一個不起作用,而第二個是的?

希望這截屏可以幫助:

https://youtu.be/UsYK93jYVqA

+0

如果您的第一個查詢不起作用,那麼執行過程中出現錯誤 –

+0

我試圖評論'$ stmt2'和'$ stmt3','$ stmt1'完美工作,我可以看到新行D B。我也嘗試將這三個陳述合併爲一個,但這沒有幫助。不知道。 – kitsune

+0

但是,答案總是相同的:如果某些數據不在數據庫中,則某些查詢失敗。如果某個查詢失敗,則出現錯誤。如果出現錯誤,您必須閱讀並解決問題。像這樣簡單 –

回答

0

問題解決了,變的:

 $stmt1->execute(); 
     $stmt2->execute(); 
     $stmt3->execute(); 

這樣:

 $stmt1->execute(); 
     $this->conn->commit(); 
     $stmt2->execute(); 
     $this->conn->commit(); 
     $stmt3->execute(); 
     $this->conn->commit(); 

有沒有爲什麼想法..但它在很多測試後都能正常工作。

+0

你的「解決方案」絕對是絕對的沒有意義 –