1.11M Members

how to delete multiple row using checkbox

 
0
 

in my previous question i only ask how can i delete a data using checkbox i forgot to put multiple check.

here's my code

<form id="form1" name="form1" method="post" action="">
                <table border='1' cellspacing='0' width='612'>
                  <tr>
                    <th bgcolor='green'><font color='white'>
                      <input type='checkbox' name='checkall' onclick='checkedAll();' />
                    </font></th>
                    <th bgcolor='green'><font color="white">ID</font></th>
                    <th bgcolor='green'><font color='white'>Check In</font></th>
                    <th bgcolor='green'><font color='white'>Check Out</font></th>
                    <th bgcolor='green'><font color='white'>Name</font></th>
                    <th bgcolor='green'><font color='white'>View</font></th>
                    <th bgcolor='green'><font color='white'>Approve</font></th>
                  </tr>
                  <?php
$i = 0; 

$number = 0;
while($row = mysql_fetch_array($result)){
while($rows = mysql_fetch_array($result1)){
$number++;

?>
                  <?php
$i++;

if($i%2)
{
$bg_color = "#EEEEEE";
}
else {
$bg_color = "#E0E0E0";
}
?>
                  <script language='JavaScript'>
      checked = false;
      function checkedAll () {
        if (checked == false){checked = true}else{checked = false}
	for (var i = 0; i < document.getElementById('form1').elements.length; i++) {
	  document.getElementById('form1').elements[i].checked = checked;
	}
      }
            </script>
                  <tr bgcolor='". $bg_color ."'>
                    <td><center>
                      <strong><font color='red'>
                        <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['cno']; ?>" />
                        </font></strong>
                    </center></td>
                    <td><center>
                      <strong><font color='red'><?php echo $rows['code']; ?></font></strong>
                    </center></td>
                    <td><center>
                      <strong><?php echo $row['checkin']; ?></strong>
                    </center></td>
                    <td><center>
                      <strong><?php echo $row['checkout'];  ?></strong>
                    </center></td>
                    <td><center>
                      <strong><font color='blue'><?php echo $rows['lname'] .', '. $rows['fname'] ?></font></strong>
                    </center></td>
                    <td><center><strong><a href='edit.php?code=<?php echo $row[cno]; ?>' class="style6">View</a></strong>
                    </center></td>
                    <td><center><Strong><a href='sendmail.php?emailadd=<?php echo $row[emailadd]; ?>' class="style6">Approve</a></Strong>
                    </center></td>
                  </tr>
                  <?php } }?>
                </table>
                <div class="select"><strong>Other Pages: </strong>
                    <select name="select">
                      <option>1</option>
                    </select>
                </div>
                <input name="delete" type="submit" id="delete" value="Delete" />
				<?php
   
      
      if(isset($_POST['delete']))
   
      {
   
      $checkbox = $_POST['checkbox']; //from name="checkbox[]"
   
      $countCheck = count($_POST['checkbox']);
   
       
   
      for($i=0;$i<$countCheck;$i++)
  
      {
  
      $del_id = $checkbox[$i];
  
       
  
      $sql = "DELETE from rent where cno = $del_id";
	  $sql1 = "DELETE from guest where code = $del_id";
  	  $result1 = mysql_query($sql1) or die (mysql_error());
      $result = mysql_query($sql) or die (mysql_error());
  
       
  
      }
  
      if($result)
  
      {
  
      header('Location: admin.php');
  
      }
  
      else
  
      {
  
      echo "Error: ".mysql_error();
  
      }
  
      }
 
      ?>

i can delete only 1 record. i want 2 or more data to delete if the checkbox is check. thanks

 
0
 

in my previous question i only ask how can i delete a data using checkbox i forgot to put multiple check.

here's my code

<form id="form1" name="form1" method="post" action="">
                <table border='1' cellspacing='0' width='612'>
                  <tr>
                    <th bgcolor='green'><font color='white'>
                      <input type='checkbox' name='checkall' onclick='checkedAll();' />
                    </font></th>
                    <th bgcolor='green'><font color="white">ID</font></th>
                    <th bgcolor='green'><font color='white'>Check In</font></th>
                    <th bgcolor='green'><font color='white'>Check Out</font></th>
                    <th bgcolor='green'><font color='white'>Name</font></th>
                    <th bgcolor='green'><font color='white'>View</font></th>
                    <th bgcolor='green'><font color='white'>Approve</font></th>
                  </tr>
                  <?php
$i = 0; 

$number = 0;
while($row = mysql_fetch_array($result)){
while($rows = mysql_fetch_array($result1)){
$number++;

?>
                  <?php
$i++;

if($i%2)
{
$bg_color = "#EEEEEE";
}
else {
$bg_color = "#E0E0E0";
}
?>
                  <script language='JavaScript'>
      checked = false;
      function checkedAll () {
        if (checked == false){checked = true}else{checked = false}
	for (var i = 0; i < document.getElementById('form1').elements.length; i++) {
	  document.getElementById('form1').elements[i].checked = checked;
	}
      }
            </script>
                  <tr bgcolor='". $bg_color ."'>
                    <td><center>
                      <strong><font color='red'>
                        <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['cno']; ?>" />
                        </font></strong>
                    </center></td>
                    <td><center>
                      <strong><font color='red'><?php echo $rows['code']; ?></font></strong>
                    </center></td>
                    <td><center>
                      <strong><?php echo $row['checkin']; ?></strong>
                    </center></td>
                    <td><center>
                      <strong><?php echo $row['checkout'];  ?></strong>
                    </center></td>
                    <td><center>
                      <strong><font color='blue'><?php echo $rows['lname'] .', '. $rows['fname'] ?></font></strong>
                    </center></td>
                    <td><center><strong><a href='edit.php?code=<?php echo $row[cno]; ?>' class="style6">View</a></strong>
                    </center></td>
                    <td><center><Strong><a href='sendmail.php?emailadd=<?php echo $row[emailadd]; ?>' class="style6">Approve</a></Strong>
                    </center></td>
                  </tr>
                  <?php } }?>
                </table>
                <div class="select"><strong>Other Pages: </strong>
                    <select name="select">
                      <option>1</option>
                    </select>
                </div>
                <input name="delete" type="submit" id="delete" value="Delete" />
				<?php
   
      
      if(isset($_POST['delete']))
   
      {
   
      $checkbox = $_POST['checkbox']; //from name="checkbox[]"
   
      $countCheck = count($_POST['checkbox']);
   
       
   
      for($i=0;$i<$countCheck;$i++)
  
      {
  
      $del_id = $checkbox[$i];
  
       
  
      $sql = "DELETE from rent where cno = $del_id";
	  $sql1 = "DELETE from guest where code = $del_id";
  	  $result1 = mysql_query($sql1) or die (mysql_error());
      $result = mysql_query($sql) or die (mysql_error());
  
       
  
      }
  
      if($result)
  
      {
  
      header('Location: admin.php');
  
      }
  
      else
  
      {
  
      echo "Error: ".mysql_error();
  
      }
  
      }
 
      ?>

i can delete only 1 record. i want 2 or more data to delete if the checkbox is check. thanks

well, you have to be creative with loops but I will tell you the primary ingredient for however you are going to do it and that is the ability to have multiple checkboxes submit into a single array. for example:

<input type="checkbox" name="delete[]" value="1" /><br />
<input type="checkbox" name="delete[]" value="2" /><br />
<input type="checkbox" name="delete[]" value="3" /><br />
<input type="checkbox" name="delete[]" value="4" /><br />
<input type="checkbox" name="delete[]" value="5" /><br />

each selected checkbox will be added to the array $_POST so you can make a loop with $_POST like so:

foreach($_POST['delete'] as $deleteID)
{
      //you are now iterating through all of the selected checkboxes and $deleteID contains the value for each checkbox
}
 
0
 

so ill create many checkbox?

 
0
 

so ill create many checkbox?

Along with a little bit of research and trial and error on your part, yes.

 
0
 

so ill create many checkbox?

no need to create you have to use for loop for this.

 
0
 

ok thanks :)

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article