10

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?

Darren Burgess
  • 4,200
  • 6
  • 27
  • 43

5 Answers5

20

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

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
4
 $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

sujal
  • 1,058
  • 3
  • 18
  • 29
4

` 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')";
Pierre-Olivier
  • 3,104
  • 19
  • 37
1

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.

Lilly_Code
  • 150
  • 1
  • 8
1

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.

4b0
  • 21,981
  • 30
  • 95
  • 142
Mohammed_7aafar
  • 385
  • 1
  • 6
  • 9