I am trying to convert the active and aftercare fields in this form to checkboxes that will post/update to the database and I cannot figure out how to make it work. I have tried several times without luck and I am hoping that someone here will be kind enough to help a newb out. I have been stumped for days. Thank you in advance for your time and input.
<?php
/
Allows the user to both create new records and edit existing records
/
// connect to the database
include("connect-db.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
// creates the new/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($first = '', $middle = '', $last = '', $ClientID = '', $IOPGroupsPresent = '', $IOPGroupsMissed = '', $error = '', $id = '')
{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php if ($id != '') { echo "Edit Record"; } ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php if ($id != '') { echo "Edit Record"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>
<form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>id: <?php echo $id; ?></p>
<?php } ?>
<strong>First Name: *</strong> <input type="text" name="FirstName"
value="<?php echo $first; ?>"/><br/>
<strong>Middle Name: </strong> <input type="text" name="MiddleName"
value="<?php echo $middle; ?>"/>
<strong>Last Name: *</strong> <input type="text" name="LastName"
value="<?php echo $last; ?>"/>
<strong>Client ID: *</strong> <input type="text" name="ClientID"
value="<?php echo $ClientID; ?>"/>
<strong>IOP Groups Present: *</strong> <input type="text" name="IOPGroupsPresent"
value="<?php echo $IOPGroupsPresent; ?>"/>
<strong>IOP GroupsMissed: *</strong> <input type="text" name="IOPGroupsMissed"
value="<?php echo $IOPGroupsMissed; ?>"/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php }
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
// get variables from the URL/form
$id = $_POST['id'];
$FirstName = htmlentities($_POST['FirstName'], ENT_QUOTES);
$MiddleName = htmlentities($_POST['MiddleName'], ENT_QUOTES);
$LastName = htmlentities($_POST['LastName'], ENT_QUOTES);
$ClientID = htmlentities($_POST['ClientID'], ENT_QUOTES);
$IOPGroupsPresent = htmlentities($_POST['IOPGroupsPresent'], ENT_QUOTES);
$IOPGroupsMissed = htmlentities($_POST['IOPGroupsMissed'], ENT_QUOTES);
// check that FirstName, LastName and ClientID are not empty
if ($FirstName == '' || $LastName == '' || $ClientID == '' || $IOPGroupsPresent == '' || $IOPGroupsMissed == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($FirstName, $MiddleName, $LastName, $ClientID, $IOPGroupsPresent, $IOPGroupsMissed, $error, $id);
}
else
{
// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE clients SET FirstName = ?, MiddleName = ?, LastName = ?, ClientID = ?, IOPGroupsPresent = ?, IOPGroupsMissed = ? WHERE id=?"))
{
$stmt->bind_param("ssssssi", $FirstName, $MiddleName, $LastName, $ClientID, $IOPGroupsPresent, $IOPGroupsMissed, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: iop_view.php");
}
}
// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// get 'id' from URL
$id = $_GET['id'];
// get the record from the database ADD TO HERE WHEN CHANGING THINGS
if($stmt = $mysqli->prepare("SELECT ID, FirstName, MiddleName, LastName, ClientID, IOPGroupsPresent, IOPGroupsMissed FROM clients WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($id, $FirstName, $MiddleName, $LastName, $ClientID, $IOPGroupsPresent, $IOPGroupsMissed);
$stmt->fetch();
// show the form
renderForm($FirstName, $MiddleName, $LastName, $ClientID, $IOPGroupsPresent, $IOPGroupsMissed, NULL, $id);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view.php page
else
{
header("Location: view.php");
}
}
}
// close the mysqli connection
$mysqli->close();
?>