2012-04-12 162 views
6

我試圖插入一條記錄到使用PDO的MySQL,我的sql語句可以在下面的代碼中看到。SQLSTATE [42S22]:未找到列:1054未知列

<?php 
    try{ 
     //include file myfunctions.php allows us to calls functions from it 
     include ("myfunctions.php"); 
     //Assign function getConnection() from myfunctions.php to variable $db 
     $db = getConnection(); 


     foreach($_POST['chk'] as $check_value) 
     { 
      $check = $check_value; 
      $fav = "channel/item [title = \"$check\"]"; 
      $holidayDoc = simplexml_load_file('holidays.xml'); 
      $favourites = $holidayDoc->xpath($fav); 

     foreach($favourites as $currentFav) 
     { 
      echo "{$currentFav->link}". "<br \>"; 
      echo "{$currentFav->title}". "<br \>"; 
      echo "{$currentFav->description}". "<br \>"; 
      echo "{$currentFav->pubDate} ". "<br \>"; 

      $sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
      VALUES (`John`, `$currentFav->link`, `$currentFav->pubDate`, `$currentFav->title`, `$currentFav->description`)"; 

      $db->exec($sql); 
      $db = null; 
     } 
    } 
} 
     catch(PDOException $e) 
     { 
      echo $e->getMessage(); 
     } 
?> 

當執行此代碼時遇到以下錯誤消息;

SQLSTATE [42S22]:列未找到:1054未知列「約翰」在 「字段列表」

這無疑是一個簡單的解決這個問題,但我似乎無法看到它,任何人都可以指出我正確的方向嗎?

回答

12

我相信這是因爲使用的是反引號爲你的價值觀使用`。其更改爲單引號和你應該是好

$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
      VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')"; 

請參考to this SO question about single quotes versus backticks如果您想了解更多信息

+0

接受大聲的時候,感謝有趣的鏈接。 – 2012-04-12 18:08:17

2
$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
      VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')"; 

僅供領域和用途「的價值觀

2

'是指定的字段,則必須使用值的單引號'

$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
     VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')"; 
相關問題