Hello, In another topic I mentioned problems with UPDATE and received advice about prepared statements so I went online for examples, then I used the information for adding data (insert) to the database. I don’t get error messages, and the page redirects (location:index.php) fine, but the information in not added to the database.
I also followed information on connecting to the database, which does work but in case I’m missing something I will start with the code:
<?php
$DB_Host = 'localhost';
$DB_User = '##########';
$DB_Pass = '';
$DB_Name = '##########';
$DB_Encoding = 'utf8';
$options = [PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC];
ini_set('display_errors',1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
try {
$dbh = new PDO("mysql:host=$DB_Host;dbname=$DB_Name;charset=$DB_Encoding",$DB_User,$DB_Pass,$options);
$dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to Database<br/>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
The following is the form - php:
<?php
if(!empty($_POST["add_record"])) {
require_once("dbcon/dbc.php");
$data =
[
'pagelinks' => $_POST['pagelinks'],
'title' => $title,
'sideleft' => $sideleft,
'body' => $body,
'asideright' => $asideright,
'sourceref' => $sourceref,
'sourceimg' => $sourceimg
];
$sql = "INSERT INTO pages ( pagelinks, title, asideleft, body, asideright, sourceref, sourceimg) VALUES (?, ?, ?, ?, ?, ?, ?)";
$pdo_statement = $dbh->prepare($sql);
$pdo_statement->execute($data);
if (!empty($result) ){
header('location:index.php');
}
header('location:read.php');
}
?>
Like I said, the database is connected, and the ‘location’ works. The information is the same as found online so I cann;t see what is wrong.
Any help will be appreciated.