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
$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
` 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')";
For anyone else struggling with this, this is why I have seen this error message and how I solved it:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With