All I want to do is create a simple page of checkboxes I need to create an array using a SQL query and compare my submitted values with those in the db. I then need to update the db to either post a 1(checked) or a 0(unchecked). however, if a checkbox isn't checked then it doesn't exist in the POST data.
here is where the array comes in How would I query a mysql db and place values into a multidimentional array? Then Hpw would I compare those values to the values submitted in my form POST variables?
I tried
while ($row = mysql_fetch_assoc ($result))
{
extract ($row);
$array['test'] = array ('test_id' => $test_id, 'test_bool' => $test_bool);
}
but that just didn't work...
here is the code I have so far if it helps...
<?php require_once('db/db.php'); ?>
<?php
mysql_select_db($database, $db);
$sql = "SELECT test_id, test_bool FROM test_table";
$result = mysql_query($sql, $db) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$totalRows = mysql_num_rows($result);
if ((isset($_POST["insert"])) && ($_POST["insert"] == "frmEnv")) {
// $sql = "INSERT INTO test_table(test_bool) VALUES('".$_POST['test_bool']."')";
// $result = mysql_query($sql, $db) or die(mysql_error());
if ((isset($_POST['chkDep'])) && ($_POST["chkDep"] != "")){
$sql = "SELECT test_id, test_bool FROM test_table";
$result = mysql_query($sql, $db) or die(mysql_error());
foreach ($_POST[chkDep] as $id => $vtest_id){
print("id: $id<br>\n");
print("vtest_id: $vtest_id<br>\n");
// NEED ARRAY AND COMPARE DONE HERE
}
}
//Header("Location: reset.php");
}
?>
<form method="post" name="frmEnv" action="">
<table align="center" border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
<table align="center" border="1" cellpadding="1" cellspacing="1">
<tr>
<td>test_id</td>
<td>test_bool</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row['test_id']; ?></td>
<td><input name="chkDep[<?php echo $row['test_id']; ?>]" type="checkbox" value="<?php echo $row['test_bool']; ?>"<?php echo ($row['test_bool']== 1 ? " checked":""); ?> ></td>
</tr>
<?php } while ($row = mysql_fetch_assoc($result)); ?>
</table>
<input type="submit" value="Submit">
<input type="hidden" name="test_id" value="">
<input type="hidden" name="insert" value="frmEnv">
</form>
<p> </p>
<?php
mysql_free_result($result);
?>
Any help would be appreciated... Thanks.
Oh Here is the DB table I'm using;
CREATE TABLE test_table (
test_id int(11) NOT NULL auto_increment,
test_bool tinyint(1) NOT NULL default '0',
PRIMARY KEY (test_id),
) TYPE=MyISAM AUTO_INCREMENT=27 ;
INSERT INTO test_table (test_id, test_bool) VALUES (1, 1),
(2, 1),
(3, 0),
(4, 0),
(5, 0),
(6, 0),
(7, 1);