Quite new to PHP and right now I'm trying to have it so when a Checkbox is checked row "cb_value" becomes 1 depending on what item the Checkbox is next to based on ID and if not checked the "cb_value" would be 0 so users can freely update a list.
I'm trying to create a Simple Test page for this with a Test Database and learn from there how to use it where I want to.
Database:
cd_id | cb_value
1 | 1
2 | 1
3 | 0
4 | 0
Right now my code is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing Checkbox Submission</title>
</head>
<?php
include "config.php";
?>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="white">
<form method="post">
<tr bgcolor="#CCCCCC" align="center">
<td width="36%"><h4>Checkbox</h4></td>
</tr>
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { // Start looping table row
$id = $row['cb_id'];
$checkbox = $row['cb_value'];
if (isset($_POST['submit'])) {
$select = mysql_query("SELECT * FROM test");
$row = mysql_fetch_assoc($select);
$id = $row['cb_id'];
$checkbox = $_POST['checkbox'];
$query = mysql_query("UPDATE test SET cb_value = '$checkbox' WHERE cb_id = '$id'");
}
?>
<tr align="center" bgcolor="#eeeeee">
<td> <input type="checkbox" name="checkbox" value="1"
<?php
if($checkbox == '1')
{
echo "checked='checked'";
}
?>/> </td>
</tr>
<?php
} // end of while loop
?>
<tr>
<td> <input type="submit" name="submit" value="Update"/> </td>
</tr>
</form>
</table>
</body>
</html>
I can't seem to wrap my head around this I've had no problems in the past updating a field in a Database via PHP but come to think about it there is normally only 1 ID involved this needs to be able to grab each one and then give it a value of either 0 or 1 depending on the state of the Checkbox.
Last time I ran the above code no matter what Checkbox I tick ID 1 ends up getting 1 in the "cb_value" and unchecking and update does not give 0 but I can't imagine it would without some form of code to say do that which I have no idea how to do right now.
Hopefully someone can help me out and explain where I'm going wrong and such.
Thanks!