当前位置: 首页 > 知识库问答 >
问题:

SQLState[42S22]:找不到列:1054未知列

晁国发
2023-03-14

我正在尝试使用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]:找不到列:1054“Field List”中的未知列“John”

这无疑是解决这个问题的一个简单方法,但我似乎看不出来,有人能给我指明正确的方向吗?

共有1个答案

莫乐
2023-03-14

我相信这是因为你在用背勾来表示你的值。将它们改为单引号,您应该会很好

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

如果您想了解更多信息,请参阅这个关于单引号和反勾号的问题

 类似资料: