0

次のエラーが表示されます: SQLSTATE[42000]: 構文エラーまたはアクセス違反: 1064 SQL 構文にエラーがあります。2 行目の「INSERT INTO student_details (student_id, first_name, last_name, dob, address_lin」の近くで使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

このコードの場合:何か考えはありますか?

//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();
}
4

3 に答える 3

0

次のように最初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
....

一度に複数のクエリを実行することは可能ですが、実行することはお勧めしません。各クエリを 1 つずつ実行すると、エラーをより適切に制御できます。

于 2013-04-13T15:47:54.253 に答える
0

PDO が複数のステートメントをサポートしているかどうかはわかりませんが、サポートしている場合、エラーは最初のステートメントを終了していないことです。

INSERT INTO student_info (student_info_id, class_id) 
VALUES (:student_info_id, :class_id);
                                    ^ add this one
于 2013-04-13T15:48:00.760 に答える
-1

1 回の呼び出しで複数のクエリを実行することはできません。
それらを 1 つずつ個別に実行します。

于 2013-04-13T15:51:11.743 に答える