[problem] check all --> delete

Discussion in 'PHP' started by zodiac, Dec 13, 2009.

  1. #1
    i check the box(s) and it deletes them,but after i still see them in the list.when i refresh the page then they're gone. :confused:
    $query = mysql_query("select id, title, thumbnail, file from games") or die(mysql_error());
    while($row = mysql_fetch_array($query)) {
    	if (!file_exists('../' . $row['thumbnail'])) {
    		echo '<form action="broken_files.php" method="POST" enctype="multipart/form-data">';
    	    echo $row['id'] . '';
    	echo '
    	<input type="checkbox" name="checkbox[]" value="'.$row['id'].'">
    
    <br />';
    
     
     }
    	}
    	 echo' 
    		<input type="submit" value="delete" />
    		</form>';
    $checked = $_POST['checkbox'];
    $count = count($checked);
    for($i=0; $i < $count; $i++)
    { 
    $query = "DELETE FROM games WHERE id='".$checked[$i]."'"; 
    mysql_query($query); 
     
    }
    PHP:

     
    zodiac, Dec 13, 2009 IP
  2. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #2
    You're deleting them *after* echoing the list to the browser. Maybe put the delete stuff in a seperate PHP file, then header() back to your list page.
     
    Wogan, Dec 13, 2009 IP
  3. mydir

    mydir Active Member

    Messages:
    352
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    @zodiac try this :)

    
    <?php
      if ($_POST)
      {
          foreach ($_POST['checkbox'] as $delete_id)
          {
              $query = 'DELETE FROM games WHERE id="'.$delete_id.'"';
              mysql_query($query);
              
              echo '<strong>'.$delete_id.'</strong> DELETED!<br />';
          }
      }
      else
      {
    ?>
    <form action="broken_files.php" method="POST">
    <?php
              $query = mysql_query("select id, title, thumbnail, file from games") or die(mysql_error());
              
              while($row = mysql_fetch_array($query))
              {
                  if (!file_exists('../' . $row['thumbnail']))
                  {
                      echo $row['id']. '<input type="checkbox" name="checkbox[]" value="'.$row['id'].'"> <br />';
                  }
              }
    ?>
    <input type="submit" value="Delete" />
    </form>
    <?php
      }
    ?>
    
    PHP:
     
    mydir, Dec 14, 2009 IP