2013-04-13 124 views
0

我收到此錯誤: SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法中有錯誤;檢查對應於你的MySQL服務器版本使用附近的「INSERT INTO student_details(student_id數據,名字,姓氏,出生日期,address_lin」第2行語法錯誤,訪問衝突pdo

此代碼正確的語法手冊:任何想法

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

您應該使用分號(';')分隔多個查詢。雖然不確定這是否是問題。 – 2013-04-13 15:46:52

+0

@navnav是的,它是:) – BlueDolphin

回答

0

我不知道,如果PDO支持多條語句,但即便如此,錯誤的是,你沒有結束的第一條語句,

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

它工作THANX :) – BlueDolphin

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 
.... 

需要注意的是,雖然它可以同時運行多個查詢,我不建議你做,如果你將運行每個查詢通過一個你可以更好地控制錯誤。

+0

嗯......你的權利 – BlueDolphin

-1

在一個呼叫,您不能運行多個查詢。
將它們分別逐一運行。

+0

你可以....我只是做了。 – BlueDolphin

+0

@BlueDolphin是的,你可以做到 – hek2mgl

相關問題