I want this form to load any data found in the "header" and "summary" columns in my database into the input fields for ease of editing. Then, after the user submits the form, the contents are dumped back into the DB and the new "values" are shown in the form. My issue is that the "dumping" part works fine, however when I refresh the page or navigate back to the form, no data is displayed and the data in the table has been replaced by white space...How do I fix this?
// Form PHP
<!-- Process POST Data and dump into DB -->
<?php
$header1 = $_POST['header1'];
$summary1 = $_POST['summary1'];
$handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE myTable SET header='$header1', summary='$summary1' WHERE id=1";
try {
$stmt = $handler->prepare($sql);
$stmt->execute();
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
$handler = null;
?>
<!-- Get mysql data -->
<?php
$handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$fetchData = $handler->prepare("SELECT * FROM newsletters WHERE id=1");
$fetchData->execute();
$data = $fetchData->fetchAll();
?>
<!-- form -->
// Form HTML
<form action="index.php" method="POST">
<input type="text" name="header1" value="<?php foreach ($data as $Data){echo $Data['header'];} ?>">
<textarea name="summary1" rows="5" value="<?php foreach ($data as $Data){echo $Data['summary'];} ?>"></textarea>
<input type="submit" name="submit1" value="Submit">
</form>