The problem: "Fatal error: Uncaught Error: Call to a member function bindParam() on string in C:\Users\Robert\Webdev_old\UniServerZ\www\PDO_DB\PDO_DB\admin.php:58 Stack trace: #0 {main} thrown in C:\Users\Robert\Webdev_old\UniServerZ\www\PDO_DB\PDO_DB\admin.php on line 58"
It appears when I try to fill in the form and send it to the database. I want all the text stored in a mysql db named "db_test" with a table named "image_gallery" with the structure:
id picName shopName displayStartDate displayEndDate uploadDate uploadPath picFile
Here is the entire code.
<?php
//require_once("inc/pdo.inc.php");
//print_r(PDO::getAvailableDrivers());
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
error_reporting(E_ERROR);
if(isset($_POST["submit"]))
{
$dsn = 'mysql:host=localhost; dbname=db_test;';
$user = 'root';
$pass = 'r00t';
// Connection in a try/catch block
try {
$DBH = new PDO($dsn, $user, $pass);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo "Bummer! Somethin' went apeshit!";
file_put_contents('PDOErrors.txt', $e->getTraceAsString(), FILE_APPEND);
};
// // initialize empty variables partly to avoid indexing issues
// $DBH = "";
// $picName = "";
// $shopName = "";
// $displayStartDate = "";
// $displayEndDate = "";
// $date = "";
// $uploadPath = "";
// $picFile = "";
// $delay = "";
try {
// prepare sql and bind parameters
$STH = new stdClass();
$STH = $DBH->prepare->query = ("INSERT INTO image_gallery ( picName, shopName, displayStartDate, displayEndDate, uploadDate, uploadPath, picFile, delay )
VALUES (:picName,:shopName,:displayStartDate,:displayEndDate,:uploadDate,:uploadPath,:picFile,:delay)"
);
$STH->bindParam(':picName', $picName);
$STH->bindParam(':shopName', $shopName);
$STH->bindParam(':displayStartDate', $displayStartDate);
$STH->bindParam(':displayEndDate', $displayEndDate);
$STH->bindParam(':uploadDate', $uploadDate);
$STH->bindParam(':uploadPath', $uploadPath);
$STH->bindParam(':picFile', $picFile);
$STH->bindParam(':delay', $delay);
$picName = $_POST['picName'];
$shopName = $_POST['shopName'];
$displayStartDate = $_POST['displayStartDate'];
$displayEndDate = $_POST['displayEndDate'];
$uploadDate = $_POST['uploadDate'];
$uploadPath = $_POST['uploadPath'];
$picFile = $_POST['picFile'];
$delay = $_POST['delay'];
// run query
$STH->execute();
} catch (Exception $e) {
echo "I'm sorry, I'm afraid I can't do that).";
}
if (!$mysqli->execute()) {
print_r($mysqli->error_list);
}
?>
The HTML form:
<form action="_admin.php" METHOD="POST">
<div class="row">
<div class="columns small-6 large-6">
<div class="medium-6 small-12 cell ">
<label>Bildnamn med kort beskrivning
<input type="text" name="picName" placeholder="Wella Conditioner 500ml">
</label>
</div>
<br>
<div class="medium-6 small-12 cell ">
<label>Butik där bild ska visas
<fieldset class="medium-6 small-12 cell">
<input name="shopName" type="checkbox"><label for="checkbox1">Alla</label>
<input name="shopName" type="checkbox"><label for="checkbox2">Malmö</label>
<input name="shopName" type="checkbox"><label for="checkbox3">3</label>
<input name="shopName" type="checkbox"><label for="checkbox1">4</label>
<input name="shopName" type="checkbox"><label for="checkbox2">5</label>
<input name="shopName" type="checkbox"><label for="checkbox3">6</label>
</fieldset>
</label>
</div>
<br>
<div class="medium-6 small-12 cell ">
<label>Startdatum för bildens visning
<input type="text" name="displayStartDate">
</label>
</div>
<br>
<div class="medium-6 small-12 cell ">
<label>Slutdatum för bildens visning
<input type="text" name="displayEndDate">
</label>
</div>
<br>
<div class="medium-6 small-12 cell ">
<label>Antal sekunder bilden ska visas
<input type="number" name="delay">
</label>
</div>
<br>
<input type="hidden" name="uploadPath">
<input type="hidden" name="uploadDate">
<input type="submit" name="submit" value="Ladda upp ➤" class="sub-style">
</div>
</div>
</form>
I coded a lot of PHP back in the late 90's, but ever since it's been on and off so I am a bit rusty. Hugely appreciate any and all help. Thank you all in advance!