PHP / MYSQL Working With Arrays...

With much help from the manual, the below is what I have so far. What I am trying to do is update multiple records in a MySQL database depending on if the user has checked off a box or not in the aforementioned record. So far what I have will query the database, and put a check in the checkbox initially if the there is a “Y” in the correct database field.

[php]<?php
$con = mysql_connect(“localhost”,“root”,“root”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

 mysql_select_db("test_db", $con);
		
 $result = mysql_query("SELECT * FROM `logincheck` LIMIT 0, 30");
		
 echo "<table border='1'>
 <tr>
 <th>Building</th>
 <th>Room Number</th>
 <th>Active</th>
 </tr>";

 while ($row = mysql_fetch_array($result))
 {
      echo "<tr>";
  echo "<td>" . $row['Building'] . "</td>";
  echo "<td>" . $row['RoomNumber'] . "</td>";

if ($row['CheckLogin'] == 'N')
{
	echo '<td><input type="checkbox" name="Active" value="'.$row['Active'].'" /></td>'."\n";
}
			
if ($row['Active'] == 'Y')
{
	echo '<td><input type="checkbox" checked="yes" name="Active" value="'.$row['Active'].'" /></td>'."\n";
}
echo "</tr>";
 }

 echo "</table>";
 echo '<input type="submit" name="button" id="button" value="Submit Changes" onClick="window.location =    \'/testing/processing.php\'" />';
 mysql_close($con);

?>[/php]

This is where I am stuck unfortunately. The submit button correctly sends to processing.php, and I put this there just so I knew it was doing something and I’d have some structure to work around.

[php]

<?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test_db", $con); $sql="INSERT INTO logincheck (Active) VALUES (0)"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); } ?>

[/php]

Whoops, I hit submit rather than preview.

In any case, I think what I’m trying to do is somehow get post data from the array, and then pick it apart as to whether that checkbox had been checked off or not. I’m having some difficulty Googling this. Learning PHP has certainly been a humbling experience.

Read this thread about check boxes

http://www.phphelp.com/forum/index.php?topic=18676.0

You need to add [] to the checkbox name (name=“Active[]”) and your checkbox code can be changed to
[php]?>

<input type=“checkbox” name=“Active[]” value="$row[‘Active’]" <?=($row['CheckLogin'] == 'N' ? "Checked = 'Checked'" : "Checked = ''")?> /> <?php[/php] Then to see what was checked, you'd do something like [php] if(isset($_POST['button'])) { for($i = 0; $i < count($_POST['active']); $i++) { // run your query in here } }[/php] I don't know what you're using the javascript for, but for this to work, you'll have to use a submit button instead.
Sponsor our Newsletter | Privacy Policy | Terms of Service