View Full Version : Using a Checkbox to update table

Grant Holmes
12-26-2007, 02:52 PM
I've a problem that I've posted in two other forums. I get 99% of what I need and posts stop. I'm new to PHP. I will ask that if you have a solution that you please tell me exactly where to use it. I've had many attempts at help that just say, "Use this"... but I know that where you put something in a loop is just as important as anything else.

I have ONE checkbox that shows the status of TINYINT field. On creation of the record, I set it at "0" (zero) to designate an "inactive" status, until a user logs in and checks the record as "Active". At this point, I'd like to check the box and have the form pass a "1" to the table.

The code that I have that shows the record on a display page correctly shows the current status of the field in the checkbox. I'm using an "edit" link to take that record to another page where all fields can be edited and UPDATEd. ALL of the fields are correctly updating the table, except the checkbox.

If the record IS active (by direct DB manipulation), I CAN click it "off" and the code WILL pass that and change the record. However, if the checkbox is OFF, I cannot get the code to pass the "1" to the database.

HELP. I feel like this should be a really simple fix as I'm so close. I really need to put this to bed.

$id = $_REQUEST["id"];
@mysql_select_db($database) or die( "Unable to select database");
if (!empty($_REQUEST["submit"])) {
if ($_REQUEST["submit"] == "Delete") {
$query = "DELETE FROM birthdays WHERE id=".$ud_id;

header("Location: suspects.php?del=1");
} else {
$query='UPDATE birthdays SET
Active = "'.$ud_Active.'",
Contact_Info_FirstName = "'.$ud_Contact_Info_FirstName.'",
Contact_Info_LastName = "'.$ud_Contact_Info_LastName.'",
Contact_Info_City = "'.$ud_Contact_Info_City.'",
Contact_Info_State = "'.$ud_Contact_Info_State.'",
Contact_Info_ZipCode = "'.$ud_Contact_Info_ZipCode.'",
Contact_Info_Country = "'.$ud_Contact_Info_Country.'"
WHERE id = "'.$ud_id.'"';
header("Location: suspects.php?id=".$ud_id); }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<TITLE>Edit Suspect</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="stylesheet" TYPE="text/css" HREF="martini.css">

$query='SELECT * FROM birthdays where id = "'.$id.'"';




echo "<b><center><b><center><H1>Martini In The Morning \"<I>Usual Suspects!</I>\"</b>";
<center><div style='width:100%; background-color:silver; text-align:right'>
<?php SECShowAdminLink(); ?>
<?php SECShowLogoutLink(); ?>&nbsp;&nbsp;
<table border="1" cellspacing="2" cellpadding="2" width="800" class="sortable">
<th width="130">Name/Address</th>
<TH valign="top" width="30">Status</TH>

while ($i < $num) {

<form method="post">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
<td valign="top"><STRONG><input type="text" name="ud_Contact_Info_FirstName" value="<? echo $Contact_Info_FirstName; ?>"><input type="text" name="ud_Contact_Info_LastName" value="<? echo $Contact_Info_LastName; ?>"></STRONG><BR>
<input type="text" name="ud_Contact_Info_City" value="<? echo $Contact_Info_City; ?>">, <input type="text" name="ud_Contact_Info_State" value="<? echo $Contact_Info_State; ?>"> <input type="text" name="ud_Contact_Info_ZipCode" value="<? echo $Contact_Info_ZipCode; ?>"> <input type="text" name="ud_Contact_Info_Country" value="<? echo $Contact_Info_Country; ?>">
<!-- ****************************I think this where my problem is.... -->
<?php $checked = $Active ? 'checked="checked"' :''; //this is a ternary - (condition) ? value if true : value if false;
echo "Active? <input type=\"checkbox\" name=\"ud_Active\" value=\"". $Active ."\" ". $checked .">";
<!-- ****************************I think this where my problem is.... -->
<TD><CENTER><input type="submit" name="submit" value="Update">
<BR><BR><input type=button value="Cancel" onClick="history.go(-1)"><BR><BR>
<input type="submit" name="submit" value="Delete" onclick="return confirm('Are you sure you want to delete this record? This cannot be undone!')"></CENTER>

echo "</table><BR>";

Grant Holmes
12-26-2007, 07:06 PM
I had one small ooboo in my code. Changing:

$checked = $Active ? 'checked' :'';
echo "<input type='checkbox'' name='Active'' value='1' $checked>";


$checked = $Active ? 'checked' :'';
echo "<input type='checkbox'' name='ud_Active'' value='1' $checked>";

The form is now working correctly.