首页 文章

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

提问于
浏览
6

我试图使用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'字段列表'中的未知列'John'

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

3 回答

  • 14

    我相信这是因为你正在使用反引号来表达你的 Value 观 . 将它们更改为单引号,你应该是好的

    $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

  • 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')";
    

相关问题