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

I am trying to insert a record in 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 get the following error message:

SQLSTATE [42S22]: column not found: 1054 Unknown John column in 'list of fields'

This is without a doubt a simple solution to this problem, but I can’t see it, can anyone point me in the right direction?

+6
source share
4 answers

I believe this is due to the fact that 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')"; 

Refer to this question about single quotes compared to reverse windows if you would like more information.

+15
source
  $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

+2
source

`to specify fields, you must use a single quotation mark ' for values.

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

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

  1. I worked on the basis of common code (there are so many participants in the work)
  2. Someone else working on the code added a column without your knowledge.
  3. You extracted the code, everything worked yesterday, but today you get the error message: "SQLSTATE [42S22]: Column not found: 1054 Unknown column" column-name "in the" list of fields ""

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

From the error message I received, I could conclude that an additional column, priceing_formula, is needed for the “properties” table. Just paste this column directly into the database.

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 someone still does not know how to solve this problem, but thinks that my explanation describes their problem, contact me - I will try to help you solve it.

Also for information, it was a Laravel backend project with a corner interface.

0
source

Source: https://habr.com/ru/post/913064/


All Articles