2016-08-22 165 views
-1

今天我遇到了一個非常奇怪的錯誤。我受到這樣糟糕的折磨,因爲它打破了我的整個應用程序。當使用PDO時使用MySQL時出現奇怪的錯誤

所以,我已經建立了這個我有一個標準模型的小框架,因此對此進行細分會有點長並且具有描述性。

<?php include('inc/inc.php'); ?> 
<?php 
if(!empty($_POST['answer']) && !empty($_POST['levelstart'])){ 
    if($stmt = $site->answerQuestion($_POST['levelstart'],  $_POST['answer'])){ 
    if($stmt[0]){ 
     echo json_encode(array('success' => true, 'correct' => $stmt[1], 'correctanswer' => $stmt[2], 'round_end' => $stmt[3])); 
    }else{ 
     echo json_encode(array('success' => false, 'error' => 'error occurred'.$stmt[1])); 
    } 
    }else{ 
    echo json_encode(array('sucess' => false, 'error' => 'Unknown error')); 
    } 
}else{ 
    echo json_encode(array('success' => false, 'error' => 'Provide all necessary parameters.')); 
} 
?> 

這段代碼輸出如下內容。

INSERT INTO quiz_level_starts (`user_id`, `question_id`, `time`, `round_id`, `type`, `success`, `ref_id`) VALUES ('4', '10', '1471887809', '', '1', '1', '905'){"success":false,"error":"error occurred23000"} 

以上生成的查詢只是一個虛擬的,我簡單地放在一起,所以我不需要簡單測試的參數化。 json數組中的「錯誤」鍵包含錯誤數據,並且錯誤代碼在那裏轉儲。 23000是存在重複唯一列的mysql錯誤代碼,但沒有在查詢中使用的唯一列(請參閱下面的表結構)。

由於函數answerQuestion是一個很長的函數,我只會貼上相關的行。在$ site-> answerQuestion中,它調用一個名爲「insertLevelStarts」的函數,它應該向db中插入一個條目。 這是我如何稱呼它:

if($stmtss = $this->db->insertLevelStarts($_SESSION['user']['id'], $stmts['return'][0]['id'], time(), $roundid, 1, 1, $levelstart)){ 

,這是它是如何宣稱,相關的和未知的代碼還剩下:

public function insertLevelStarts($user_id, $question_id, $time, $round_id, $type = 0, $success = 0, $refid = 0){ 
    /* 
    Type=0 start 1 stop 
    success=0 for start 1 if successfull on stop 
    */ 
    $query = 'INSERT INTO quiz_level_starts (`user_id`, `question_id`, `time`, `round_id`, `type`, `success`, `ref_id`) VALUES (:user_id, :question_id, :time, :round_id, :type, :success, :refid)'; 
    echo $this->genFakeQuery($query, array(
     ':user_id' => $user_id, 
     ':question_id' => $question_id, 
     ':time' => $time, 
     ':type' => $type, 
     ':success' => $success, 
     ':refid' => $refid, 
     ':round_id' => $round_id 
    )); 
    return $this->execInsert($query, array(
     ':user_id' => $user_id, 
     ':question_id' => $question_id, 
     ':time' => $time, 
     ':type' => $type, 
     ':success' => $success, 
     ':refid' => $refid, 
     ':round_id' => $round_id 
    ) 
); 
} 
public function genFakeQuery($query, $array){ 
    foreach($array as $key => $val){ 
    $query = str_replace($key, "'$val'", $query); 
    } 
    return $query; 
} 
public function execUpdate($query, $preparray, $updatearr){ 
    try { 
     $stmt = $this->db->prepare($query); 
     $stmt->execute(array_merge($preparray, $updatearr)); 
     $rows = $stmt->rowCount(); 
     if($rows > 0){ 
      return array('type' => 'rowsaffected', 'return' => $rows); 
     }else{ 
      return array('type' => 'noreturn', 'return' => 'none'); 
     } 
    } catch(PDOException $ex) { 
     return array('type' => 'error', 'return' => $ex); 
    } 
} 
public function updateClause($query, $update, $updatearr){ 
    if(count($update) > 0){ 
    $count = 0; 
    foreach($update as $k => $v){ 
     if($count > 0){ 
     $query .= ','; 
     } 
     $query .= " `$k` = :$k"; 
     $updatearr[":$k"] = $v; 
     $count++; 
    } 
    } 
    return array('query' => $query, 'updatearr' => $updatearr); 
} 

上述查詢

INSERT INTO quiz_level_starts (`user_id`, `question_id`, `time`, `round_id`, `type`, `success`, `ref_id`) VALUES ('4', '10', '1471887809', '', '1', '1', '905') 

刀片放入如下所示的表格中:

CREATE TABLE IF NOT EXISTS `quiz_level_starts` (
`id` int(11) NOT NULL, 
`user_id` int(11) NOT NULL, 
`question_id` int(11) NOT NULL, 
`time` int(11) NOT NULL, 
`type` int(11) NOT NULL, 
`success` int(11) NOT NULL, 
`ref_id` int(11) NOT NULL, 
`round_id` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
ALTER TABLE `quiz_level_starts` 
ADD PRIMARY KEY (`id`); 
ALTER TABLE `quiz_level_starts` 
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; 

將大大appriciate接受任何幫助。

+3

好吧,錯誤代碼23000可以有不同的含義http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html –

+2

您需要從mysql打印更詳細的錯誤信息 –

回答

0

我假定發生錯誤是因爲round_id是一個整數字段,它不能爲NULL並且沒有默認值,並且您將它傳遞給一個空值。 嘗試,如果此查詢的工作:

INSERT INTO quiz_level_starts (`user_id`, `question_id`, `time`, `round_id`, `type`, `success`, `ref_id`) VALUES ('4', '10', '1471887809', '0', '1', '1', '905') 
相關問題