Hello,
I'm having a problem determining how to pass a value from a form with a checkbox on a html page to a mysql database. I've preloaded some data and can populate a box if the data is already entered. But I would like to be able to check the box or uncheck the box and pass that value to the db (being able to change the value depending on whether it's checked or not). I'm a newbie to php so please bare with me.
My code follows:
<body>
<?php
$db = mysql_connect("myweb");
mysql_select_db("faxlist",$db);
$count = mysql_query("SELECT COUNT(*) FROM members")
or exit();
if ($row = mysql_fetch_array ($count))
echo "<P>The list currently has " . $row[0] . " members.<BR> <br>";
if ($submit) {
// here if no member_id then adding else we're editing
if ($member_id) {
$sql = "UPDATE members SET first_name='$first_name',last_name='$last_name',address='$address',company='$company',faxlist='$faxlist',city='$city' WHERE member_id=$member_id";
} else {
$sql = "INSERT INTO members (first_name,last_name,address,company,faxlist,city) VALUES ('$first_name','$last_name','$address','$company','$faxlist','$city')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM members WHERE member_id=$member_id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$member_id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM members ORDER BY last_name",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=http://www.phpbuilder.com/board/archive/index.php/\"%s?member_id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["member_id"], $myrow["first_name"], $myrow["last_name"]);
printf("<a href=\"%s?member_id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["member_id"]);
}
}
?>
<P>
<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($member_id) {
// editing so select a record
$sql = "SELECT * FROM members WHERE member_id=$member_id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$member_id = $myrow["member_id"];
$first_name = $myrow["first_name"];
$last_name = $myrow["last_name"];
$address = $myrow["address"];
$city = $myrow["city"];
$company = $myrow["company"];
$faxlist = $myrow["faxlist"];
// print the member_id for editing
?>
<input type=hidden name="member_id" value="<?php echo $member_id ?>">
<?php
}
?>
First:<input type="Text" name="first_name" value="<?php echo $first_name ?>"><br>
Last:<input type="Text" name="last_name" value="<?php echo $last_name ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
City:<input type="Text" name="city" value="<?php echo $city ?>"><br>
company:<input type="Text" name="company" value="<?php echo $company ?>"><br>
Faxlist:<input type="checkbox" name="faxlist" value=<?php echo $faxlist ?> <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
?>
</body>
Thanks you
I'm having a problem determining how to pass a value from a form with a checkbox on a html page to a mysql database. I've preloaded some data and can populate a box if the data is already entered. But I would like to be able to check the box or uncheck the box and pass that value to the db (being able to change the value depending on whether it's checked or not). I'm a newbie to php so please bare with me.
My code follows:
<body>
<?php
$db = mysql_connect("myweb");
mysql_select_db("faxlist",$db);
$count = mysql_query("SELECT COUNT(*) FROM members")
or exit();
if ($row = mysql_fetch_array ($count))
echo "<P>The list currently has " . $row[0] . " members.<BR> <br>";
if ($submit) {
// here if no member_id then adding else we're editing
if ($member_id) {
$sql = "UPDATE members SET first_name='$first_name',last_name='$last_name',address='$address',company='$company',faxlist='$faxlist',city='$city' WHERE member_id=$member_id";
} else {
$sql = "INSERT INTO members (first_name,last_name,address,company,faxlist,city) VALUES ('$first_name','$last_name','$address','$company','$faxlist','$city')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM members WHERE member_id=$member_id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$member_id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM members ORDER BY last_name",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=http://www.phpbuilder.com/board/archive/index.php/\"%s?member_id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["member_id"], $myrow["first_name"], $myrow["last_name"]);
printf("<a href=\"%s?member_id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["member_id"]);
}
}
?>
<P>
<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($member_id) {
// editing so select a record
$sql = "SELECT * FROM members WHERE member_id=$member_id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$member_id = $myrow["member_id"];
$first_name = $myrow["first_name"];
$last_name = $myrow["last_name"];
$address = $myrow["address"];
$city = $myrow["city"];
$company = $myrow["company"];
$faxlist = $myrow["faxlist"];
// print the member_id for editing
?>
<input type=hidden name="member_id" value="<?php echo $member_id ?>">
<?php
}
?>
First:<input type="Text" name="first_name" value="<?php echo $first_name ?>"><br>
Last:<input type="Text" name="last_name" value="<?php echo $last_name ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
City:<input type="Text" name="city" value="<?php echo $city ?>"><br>
company:<input type="Text" name="company" value="<?php echo $company ?>"><br>
Faxlist:<input type="checkbox" name="faxlist" value=<?php echo $faxlist ?> <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
?>
</body>
Thanks you