2015-07-20 34 views
-2

由於某種原因,我收到此錯誤,但我不在尋找名爲aaron的列。Php PDO致命錯誤 - 列未找到,但我不看

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Aaron' in 'field list'' in /home/stretch045/public_html/scripts/auth.php:15 Stack trace: #0 /home/stretch045/public_html/scripts/auth.php(15): PDO->prepare('INSERT INTO use...') #1 /home/stretch045/public_html/index.php(35): Auth->checkToken('94257b73ea4ed51...') #2 {main} thrown in /home/stretch045/public_html/scripts/auth.php on line 15

CODE:

$conn = $this->db; 
$stmt = $conn->prepare("UPDATE users SET rating='".$xml->rating."', atc='".$xml->ratingatc."', pilot='".$xml->ratingpilot."', division='".$xml->division."' WHERE vid='".$xml->vid."'"); 
$stmt->execute(); 
if($stmt->rowCount()==0){ 
    $stmt = $conn->prepare("INSERT INTO users (vid, fname, lname, rating, atc, pilot, division) VALUES (".$xml->vid.",".$xml->firstname.",".$xml->lastname.",".$xml->rating.",".$xml->ratingatc.",".$xml->ratingpilot.",".$xml->division.")"); 
    $stmt->exec($stmt); 
    echo 'data inserted into db'; 
} 
+0

在插入查詢中,沒有任何字段值包含在單引號中。 MySQL會將這些值解釋爲列名。 – 2015-07-21 00:05:07

+0

'$ stmt-> exec($ stmt)'不是你需要在那裏做的。應該是'$ stmt-> execute()',正如Jessica在下面所建議的那樣,execute()參數應該是該語句中的一個命名參數數組,它解決了原始問題。 –

回答

4

在你的第二個查詢,你沒有把周圍的字符串引號。所以它會將包含'aaron'的變量視爲一列。

這可以通過實際使用參數化查詢來最好的解決。

$query = "INSERT INTO users (vid, fname, ...) VALUES (:vid, :fname, ...)"; 
$stmt = $conn->prepare($query); 
$stmt->execute(['vid'=>$xml->vid, 'fname'=>$xml->fname, ...]); 
相關問題