Undefined Index in PHP Checkbox
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<table border="1" cellpadding="4">
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<?php
include('connect.php');
$getQuery = "SELECT id,username,admin FROM tutorial_users ORDER BY
id ASC";
$getResult = mysql_query($getQuery) or die("Query not executed");
while (list($id, $userName, $admin) = mysql_fetch_row($getResult)) {
?>
<?php
$checked = ($admin == 1) ? 'checked="checked"' : '';
?>
<tr align="center">
<td><?php echo $id; ?></td>
<td><?php echo $userName; ?></td>
<td><?php echo "<input type='checkbox' name='admin[]'
value='$id' $checked>"; ?></td>
<?php } ?>
<tr>
</table>
<p>
<input type="submit" name="submit" value="Update Role">
</p>
</form>
<?php
$updated = FALSE;
if (isset($_POST['submit'])) {
$admin = $_POST['admin'];
$admin = join(",", $admin);
$admin = "(" . $admin . ")";
$defaultQuery = "UPDATE tutorial_users SET admin=0";
$defaultResult = mysql_query($defaultQuery);
$upQuery = "UPDATE tutorial_users SET admin=1 WHERE id IN $admin";
echo $upQuery;
$upResult = mysql_query($upQuery);
$updated = TRUE;
}
?>
In the above code I have 2 questions: 1- Lets say i want every check box
to be unchecked and then when i press the submit it give undefined index
$admin. How can i fix this. It is working fine when i m checking atleast
one box and it passes the value to the POST array.
2 - Secondly i would like to know that is this a good approach to achieve
result or we can have some other way ?? Personaly i m feeling it bad
because if i have 1000 record then it will take alot of time to update 2
queries one by one...
Result i want is pretty simple that the checked box should have value = 1
where as unchecked have 0.
Thanks
No comments:
Post a Comment