Checkbox Confusion

Hi guys, just after a little help here…

Ive set up this form to update multiple fields at the same time, however, i have a tick box in the form and whenever i put a tick in one of the rows it always submits the query with the tick box in the first row… - not the row selected…

heres the code:


	// Connect to server and select databse.
	mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
	mysql_select_db("$db_name")or die("cannot select DB");

	$sql="SELECT * FROM $tbl_name WHERE auth='0'";
	$result=mysql_query($sql);

	// Count table rows 
	$count=mysql_num_rows($result);
	?>
	<table width="500" border="0" cellspacing="1" cellpadding="0">
	<form name="form1" method="post" action="">
	<tr> 
	<td>
	<table width="500" border="0" cellspacing="1" cellpadding="0">


	<tr>
	
	<td align="center"><strong>Name</strong></td>
	<td align="center"><strong>Lastname</strong></td>
	<td align="center"><strong>Time (hh:mm:ss)</strong></td>
	<td align="center"><strong>Email</strong></td>
	<td align="center"><strong>Authorize</strong></td>
	</tr>
	<?php
	while($rows=mysql_fetch_array($result)){
	?>
	<tr>
	<?php $id[]=$rows['id']; ?><input name="id[]" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
	<td align="center"><?php echo $rows['name']; ?></td>
	<td align="center"><?php echo $rows['lastname']; ?></td>
	<td align="center"><input name="time[]" type="text" id="time" value="<?php echo $rows['time']; ?>" class="text input_mask mask_time" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'<?php echo $rows['time']; ?>':this.value;"></td>
	<td align="center"><?php echo $rows['email']; ?></td>
	<td align="center"><input type="checkbox" name="auth[]" value="<?php echo $rows["auth"]; ?>" onclick="this.value='2';"></td>
	</tr>
	<?php
	}
	?>
	<tr>
	<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
	</tr>
	</table>
	</td>
	</tr>
	</form>
	</table>
	<?php
	// Check if button name "Submit" is active, do this 
	if($_POST['Submit']){
	for($i=0;$i<$count;$i++){

	$id = $_POST['id'][$i];
	//$name = $_POST['name'][$i];
	//$lastname = $_POST['lastname'][$i];
	$time = $_POST['time'][$i];
	//$email = $_POST['email'][$i];
	$auth = $_POST['auth'][$i];

	$sql1="UPDATE $tbl_name SET time='$time',auth='$auth' WHERE id='$id'";
	$result1=mysql_query($sql1) or die(mysql_error());
	}
	}

	if($result1){
	header("location:update_multiple.php");
	}
	mysql_close();
	?>

any ideas where i might be going wrong?

Thanks.

Just an addition to this, if i change the checkbox in the above code to type=“text” and use the value of ‘1’ in the text box it works perfectly. Its something to do with the check box itself but i cant see how or why…

Checkboxes are only detectable in the form data if they are indeed checked, so does this yield the result you seek?


// ternary instead of if-else 
// change 1, 0 to suit values in your table
$auth = ( isset( $_POST['auth'][$i] ) ) ? 1 : 0;


else use

var_dump( $_POST );

to see what PHP is being passed.

Basically you need something like this:
type=“checkbox” name=“auth[<?php echo $row[‘id’]; ?>”
That gets you around the problem that checkboxes are only submitted when they are checked.
Your controller code can then do something like:
foreach($_POST[‘auth’] as $id => $value) …

Post the actual html your php code generates so we can see more clearly what is going on.