I'm trying to delete mutiple records upon a selection of check boxes, but the scipt does not delete records if i select more than one, but insteads deletes only one record. Here is the code: updateuser. php php: <?PHP include 'opendb.php'; ?> <?PHP include 'header.php'; ?> <?PHP //$checkbox; $sql="SELECT * FROM Users"; $result=mssql_query($sql); $count=mssql_num_rows($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action="Userdelete.php"> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="3" bgcolor="#FFFFFF"><strong>Delete Users</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>UserID</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>UserName</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>UserInitials</strong></td> </tr> <?php while($rows=mssql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['UserID']; ?>"></td> <td bgcolor="#FFFFFF"><?php echo $rows['UserID']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['UserName']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['UserInitials']; ?></td> </tr> <?php } ?> <tr> <td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="delete"></td> </tr> <?php // Check if delete button active, start this $checkbox="checkbox[]"; if($delete = 'delete'){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sqld = "DELETE FROM Users WHERE UserID='$del_id'"; //$result3 = mssql_query($sqld); } // if successful redirect to delete_multiple.php if($result3 = mssql_query($sqld) ){ echo "<meta http-equiv= content=\"0;URL=Userdelete.php\">"; } else echo"Problem deleting"; } mssql_close(); ?> </table> </form> </td> </tr> </table> Userdelete.php php: <?PHP include 'opendb.php'; ?> <?PHP include 'header.php'; ?> <?php $UserName = 'UserName' $UserInitials = $_GET['UserInitials']; $UserID = $_GET['UserID']; $query="UPDATE Users SET UserName='$UserName', UserInitials='$UserInitials' WHERE UserID='$UserID'"; mssql_query($query); echo "Record Updated"; mssql_close(); ?> <?PHP include 'footer.php'; ?>
mysql_query("DELETE FROM Users WHERE UserID IN(" . implode(', ', array_map('intval', (array)$_POST['checkbox'])) .")") OR die(mysql_error()); PHP:
hello use this no need count all mysql num rows $count=mssql_num_rows($result); count only checkboxs submited $count=count($_POST['checkbox']); for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sqld = "DELETE FROM Users WHERE UserID='$del_id'"; //$result3 = mssql_query($sqld); }