Ok, so I got this to work, however, I feel it's very sloppy and would enjoy suggestions/help..
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($id, $highlightID, $highlight, $error)
{
?>
<?php
$highlightCount = 6;
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<?php
for($i=1; $i<=$highlightCount; $i++){
echo '<p><input type="hidden" name="highlightID['.$i.']" value="'.@$highlightID[$i].'"/></p>';
echo '<p><input type="text" placeholder="Highlight '.$i.'" name="highlight['.$i.']" value="'.@$highlight[$i].'"/></p>';
}
?>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
// connect to the database
include('DB.class.php');
$highlightCount = 6;
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$highlightID = $DB->clean($_POST['highlightID']);
$highlight = $DB->clean($_POST['highlight']);
// check that firstname/lastname fields are both filled in
if (@$error)
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $highlightID, $highlight, $error);
} else {
for($i=1; $i<=6; $i++){
// save the data to the database
if(!empty($highlight[$i])){
if(isset($highlightID[$i])){
$DB->Query("UPDATE highlights SET highlight='$highlight[$i]', property_id_ref='$id' WHERE highlight_id='$highlightID[$i]'", "highlights")
or die(mysql_error());
} else {
$DB->Query("INSERT INTO highlights(property_id_ref, highlight) VALUES ('$id', '$highlight[$i]')", "highlight")
or die(mysql_error());
}
}
}
// once saved, redirect back to the view page
echo "success!";
//header("Location: view.php");
}
} else {
// if the 'id' isn't valid, display an error
echo 'Error!';
}
// if the form hasn't been submitted, get the data from the db and display the form
} else {
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = $DB->query("SELECT * FROM highlights WHERE property_id_ref=$id", 'highlights')
or die(mysql_error());
while($row = $DB->Fetch('highlights')){
// get data from db
$highlightID[] = $row['highlight_id'];
$highlight[] = $row['highlight'];
}
// show form
renderForm($id, $highlightID, $highlight, '');
} else {
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
echo 'Error!';
}
}
?>
</body>
</html>