简体   繁体   中英

SQLSTATE[42S22]: Column not found: 1054 Unknown column

I am attempting to insert a record to MySQL using PDO, my sql statement can be seen in the following code.

<?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();
        }
?>

When this code is executed i am met with the following error message;

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'John' in 'field list'

This is no doubt a simple solution to this problem but i cannot seem to see it, can anyone point me in the right direction?

I believe this is because you are using backticks for your values. Change them to single quotes and you should be good

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

Please refer to this SO question about single quotes versus backticks if you want more information

` is for specifying fields, you must use a single quote ' for values.

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

Use ` for fields only and use ' for values

For anyone else struggling with this, this is why I have seen this error message and how I solved it:

  1. I was working on a shared code base (in work so many contributors)
  2. Someone else working on the code added a column without you knowing.
  3. I pulled the code, all was working yesterday but today I got the error message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'column_name' in 'field list' "

All you have to do is go to your table and add the Column (for me it was in phpmyadmin that I did this).

From the error message I received, I could deduce it was 'properties' table that needed the additional column pricing_formula. Just insert this column directly into the DB.

For reference, this was my error message:

"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)"

Hope this helps someone. If anyone still doesn't fully know how to fix this issue but thinks my explanation describes their problem, reach out - I will try to help you fix it.

Also for info, this was a Laravel backend project with an Angular front end.

Update your model with this:

public $timestamps = false;

This happened because Eloquent function adds 'updated at' property to your query which is timestamp and this will throw this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM