【问题标题】:syntax error, access violation pdo语法错误,访问冲突 pdo
【发布时间】:2013-04-06 01:24:45
【问题描述】:

我收到此错误: SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的“INSERT INTO student_details (student_id, first_name, last_name, dob, address_lin') 附近使用正确的语法

对于这段代码:有什么想法吗?

//create variables from each value that was submitted from the form */
$student_info_id = $_POST['student_info_id'];
$class_id = $_POST['class_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$dob = $_POST['dob'];
$address_line_1 = $_POST['address_line_1'];
$address_line_2 = $_POST['address_line_2'];
$town = $_POST['town'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$gender = $_POST['gender'];
$ethnicity = $_POST['ethnicity'];


try {
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $password);
$conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $sql = "
        INSERT INTO student_info (student_info_id, class_id) VALUES (:student_info_id, :class_id) 
        INSERT INTO student_details (student_id, first_name, last_name, dob, address_line_1, address_line_2, town, county, postcode, gender, ethnicity, student_info_id)
                                        VALUES (:student_id, :first_name, :last_name, :dob, :address_line_1, :address_line_2, :town, :county, :postcode, :gender, :ethnicity, :student_info_id)     

        ";

 $statement = $conn->prepare($sql);
 $statement->bindValue(":student_info_id", $student_info_id);
 $statement->bindValue(":class_id", $class_id);
 $statement->bindValue(":student_id", $student_id);
 $statement->bindValue(":first_name", $first_name);
 $statement->bindValue(":last_name", $last_name);
 $statement->bindValue(":dob", $dob);
 $statement->bindValue(":address_line_1", $address_line_2);
 $statement->bindValue(":address_line_2", $address_line_1);
 $statement->bindValue(":town", $town);
 $statement->bindValue(":county", $county);
 $statement->bindValue(":postcode", $postcode);
 $statement->bindValue(":gender", $gender);
 $statement->bindValue(":ethnicity", $ethnicity);
 $statement->bindValue(":student_info_id", $student_info_id);

 $count = $statement->execute();

  $conn = null;        // Disconnect
}
catch(PDOException $e) {
  echo $e->getMessage();
}

【问题讨论】:

  • 您应该使用分号 (;) 来分隔多个查询。不过,不确定这是否是问题所在。
  • @navnav 是的 :)

标签: php mysql pdo


【解决方案1】:

我不确定PDO是否支持多条语句,但如果支持,错误是你没有终止第一个语句,

INSERT INTO student_info (student_info_id, class_id) 
VALUES (:student_info_id, :class_id);
                                    ^ add this one

【讨论】:

    【解决方案2】:

    您必须使用; 完成第一个INSERT,如下所示:

    INSERT INTO student_info (
       student_info_id,
       class_id
    ) VALUES (
       :student_info_id,
       :class_id
    ); <-- a semicolon is the default statement separator, use it
    ....
    

    请注意,虽然可以同时运行多个查询,但我不建议您这样做。如果您逐个运行每个查询,您将更好地控制错误。

    【讨论】:

      【解决方案3】:

      您不能在一次调用中运行多个查询。
      一个一个地单独运行它们。

      【讨论】:

      • @BlueDolphin 是的,你可以做到
      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2017-07-26
      相关资源
      最近更新 更多