8

我正在尝试使用 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'

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

4

5 回答 5

20

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

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

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

于 2012-04-12T18:01:54.763 回答
4

` 用于指定字段,您必须对值使用单引号'

$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
        VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";
于 2012-04-12T18:02:06.407 回答
3
 $sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
            VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";

仅将 ` 用于字段,将 ' 用于值

于 2012-04-12T18:01:45.190 回答
1

对于其他为此苦苦挣扎的人,这就是我看到此错误消息以及如何解决它的原因:

  1. 我正在研究一个共享代码库(在工作中有很多贡献者)
  2. 其他处理代码的人在您不知情的情况下添加了一个列。
  3. 我拉了代码,昨天一切正常,但今天我收到错误消息:“SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'column_name'”

您所要做的就是去您的表并添加列(对我来说,我是在 phpmyadmin 中执行此操作的)。

从我收到的错误消息中,我可以推断出需要附加列pricing_formula 的“属性”表。只需将此列直接插入数据库即可。

作为参考,这是我的错误信息:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pricing_formula' in 
'field list' (SQL: select `site`.`name`, `domain`, `site`.`uuid`, 
`pricing_formula`, `client`.`uuid` as `clientRef` from `site` inner join 
`client` on `site`.`client_id` = `client`.`id` where `client_id` = 1 and 
`site`.`deleted_at` is null)"

希望这可以帮助某人。如果有人仍然不完全知道如何解决此问题,但认为我的解释描述了他们的问题,请联系 - 我会尽力帮助您解决问题。

此外,这是一个带有 Angular 前端的 Laravel 后端项目。

于 2019-01-24T12:24:11.110 回答
1

用这个更新你的模型:

public $timestamps = false;

发生这种情况是因为 Eloquent 函数将 'updated at' 属性添加到您的查询中,即时间戳,这将引发此错误。

于 2020-03-19T06:42:33.287 回答