我正在尝试使用 PDO 向 MySQL 中插入一条记录,我的 sql 语句可以在以下代码中看到。
<?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]: Column not found: 1054 Unknown column 'John' in 'field list'
这无疑是解决这个问题的一个简单方法,但我似乎看不到它,任何人都可以指出我正确的方向吗?
最佳答案
我相信这是因为您正在为您的值使用反引号。将它们更改为单引号,你应该很好
$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如果你想了解更多信息
关于php - SQLSTATE[42S22] : Column not found: 1054 Unknown column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10129120/