Hi!
I have posted this in another forum, but haven't got an answer. Perhaps because the question doesn't make sense? If so, I'd appreciate if I could be pointed towards making sense ... :-)
I am populating/checking a checkbox if a database field value is 1. I want to edit the values in the database via a form; I want the database field value set to NULL if unchecking the checkbox. This editing works when manipulating the value via a text field, but not when manipulating via a checkbox. (The checkbox displays correctly (i.e. as checked when the database field value is 1, otherwise as unchecked), but the updating when using the checkbox doesn't work. I have tried with different data types i the DB. NULL is standard value.
Any ideas what is causing this? Is is something wrong in the UPDATE statement that doesn't apply to checkboxes? (If that question makes sense...)
Thank you for any help!
This is the update part:
# Iterate through each field in the form, using the values to
# construct the UPDATE statement.
$stmt = "UPDATE member ";
$delim = "SET";
reset ($row);
while (list ($col_name, $val) = each ($row))
{
$stmt .= "$delim $col_name=";
$delim = ",";
# if a form value is empty, update the corresponding column value
# with NULL if the column is nullable. This prevents trying to
# put an empty string into the expiration date column when it
# should be NULL, for example.
$val = trim ($val);
if (empty ($val))
{
if ($nullable[$col_name])
$stmt .= "NULL"; # enter NULL
else
$stmt .= "''"; # enter empty string
}
else
$stmt .= $conn->quoteSmart ($val);
}
$stmt .= sprintf (" WHERE member_id = %s", $conn->quoteSmart ($member_id));
$result =& $conn->query ($stmt);
if (DB::isError ($result))
print ("Oppdatering feilet.\n");
else
print ("Oppdatering utført.\n");
printf ("<br /><a href=\"%s\">Oppdatér en annen instans.</a>\n",
script_name ());
This is the function writing the checkbox
function checkbox ($name, $value, $checked)
{
printf ("<input type=\"%s\" name=\"%s\" value=\"%s\"%s />%s\n",
"checkbox",
htmlspecialchars ($name),
htmlspecialchars ($value),
($checked = ($value==1) ? ' checked="checked"' : ''),
htmlspecialchars ($label));
}