I am using the script below to store values for dynamically created forms in to a MySQL database. Users can return to the form after it is saved to update information.
The application uses radio button form elements, which are only sent through POST if they are answered. The default value in the MySQL database is set to NULL. So if a question is not answered, it remains NULL, which is great.
However, I also use jQuery to allow certain form elements to be reset (i.e. radio buttons can be cleared).
If a user answers a radio button question, saves the form, returns at a later date, and clears the radio button question, the value never gets set back to NULL in my database.
Any ideas on how I can make this happen?
<?php
require_once('../../includes/connect.php');
$org_id = $_SESSION['ORG_ID'];
$user_id = $_SESSION['USER_ID'];
$dbh = get_org_dbh($org_id);
$sql = "UPDATE health_forms SET ";
$values = array();
foreach ($_POST as $field => $value){
$sql .= "$field=?, ";
array_push($values, $value);
}
$sql = rtrim($sql,", ");
$sql .= " WHERE id=?;
";
array_push($values, $user_id);
try {
$sth = $dbh->prepare($sql);
$sth->execute($values);
}
header("location: ../index.php?i=completed");
?>