Php 使用PDO时使用MySQL时出现奇怪错误

Php 使用PDO时使用MySQL时出现奇怪错误,php,mysql,sql,Php,Mysql,Sql,我今天遇到了一个很奇怪的虫子。这让我左右为难,因为它破坏了我的整个应用程序 所以,我有一个我构建的小框架,其中有一个标准的model,所以代码片段将有点长和描述性 <?php include('inc/inc.php'); ?> <?php if(!empty($_POST['answer']) && !empty($_POST['levelstart'])){ if($stmt = $site->answerQuestion($_POST['leve

我今天遇到了一个很奇怪的虫子。这让我左右为难,因为它破坏了我的整个应用程序

所以,我有一个我构建的小框架,其中有一个标准的model,所以代码片段将有点长和描述性

<?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.'));
}
?>
上面生成的查询只是我简单地组合在一起的一个伪查询,因此我不需要参数化来进行简单的测试。json数组中的“error”键包含错误数据,错误代码被转储到那里。 23000是mysql的错误代码,因为存在重复的唯一列,但我在查询中没有使用唯一列(请参见下面的表struct)

由于函数answerQuestion是一个很长的问题,接下来我只粘贴相关的行。在$site->answerQuestion中,它调用一个名为“insertLevelStarts”的函数,该函数应该向数据库插入一个条目。 我这样称呼它:

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;

将非常感谢收到的任何帮助。

我假设发生错误是因为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')

错误代码23000可能有不同的含义,您需要从mysql打印更详细的错误消息
INSERT INTO quiz_level_starts (`user_id`, `question_id`, `time`, `round_id`, `type`, `success`, `ref_id`) VALUES ('4', '10', '1471887809', '0', '1', '1', '905')