delete multiple checkbox coding

  • kkK
  • Born
  • Born
  • kkK
  • Posts: 1

Post 3+ Months Ago

Code: [ Select ]
[
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// 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";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php
 
  // Check if delete button active, start this
 
  $delete = $_REQUEST['delete'];
 
  if( $delete != '' ){
 
   
 
   foreach( $checkbox as $del_id ){
 
     $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
 
     $result = mysql_query($sql);
 
   
 
   }
 
 
 
   // if successful redirect to delete_multiple.php
 
   if($result){
 
     echo "<html><head><meta HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=delete_multiple.php\"></html>";
 
   }
 
  }
 
?>
</table>
</form>
</td>
</tr>
</table>
  1. [
  2. <?php
  3. $host="localhost"; // Host name
  4. $username="root"; // Mysql username
  5. $password=""; // Mysql password
  6. $db_name="test"; // Database name
  7. $tbl_name="test_mysql"; // Table name
  8. // Connect to server and select databse.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11. $sql="SELECT * FROM $tbl_name";
  12. $result=mysql_query($sql);
  13. $count=mysql_num_rows($result);
  14. ?>
  15. <table width="400" border="0" cellspacing="1" cellpadding="0">
  16. <tr>
  17. <td><form name="form1" method="post" action="">
  18. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
  19. <tr>
  20. <td bgcolor="#FFFFFF">&nbsp;</td>
  21. <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
  22. </tr>
  23. <tr>
  24. <td align="center" bgcolor="#FFFFFF">#</td>
  25. <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  26. <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  27. <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
  28. <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
  29. </tr>
  30. <?php
  31. while($rows=mysql_fetch_array($result)){
  32. ?>
  33. <tr>
  34. <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
  35. <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
  36. <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
  37. <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
  38. <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  39. </tr>
  40. <?php
  41. }
  42. ?>
  43. <tr>
  44. <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
  45. </tr>
  46. <?php
  47.  
  48.   // Check if delete button active, start this
  49.  
  50.   $delete = $_REQUEST['delete'];
  51.  
  52.   if( $delete != '' ){
  53.  
  54.    
  55.  
  56.    foreach( $checkbox as $del_id ){
  57.  
  58.      $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  59.  
  60.      $result = mysql_query($sql);
  61.  
  62.    
  63.  
  64.    }
  65.  
  66.  
  67.  
  68.    // if successful redirect to delete_multiple.php
  69.  
  70.    if($result){
  71.  
  72.      echo "<html><head><meta HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=delete_multiple.php\"></html>";
  73.  
  74.    }
  75.  
  76.   }
  77.  
  78. ?>
  79. </table>
  80. </form>
  81. </td>
  82. </tr>
  83. </table>


i need help....
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6229
  • Loc: South-Africa

Post 3+ Months Ago

What do you need help with? Please be more specific.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 50 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.