0
votes

i am getting this error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO student_details (student_id, first_name, last_name, dob, address_lin' at line 2

for this code: any idea?

//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();
}
3
You should use semi-colons (;) to separate multiple queries. Not sure if that's the issue, here, though.user1941719
@navnav yes it was :)BlueDolphin

3 Answers

0
votes

I'm not sure if PDO support multiple statements, but if so, the error is that you did not terminate the first statement,

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

You'll have to finish the first INSERT with a ; Like this:

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

Note that, although it is possible to run multiple queries at once, I would not advice you to do it. If you would run each query one by one you would have a better control over errors.

-1
votes

You can't run multiple queries in one call.
Run them separately, one by one.