Search code examples
phpmysqlbulk

Bulk action using PHP for mysql entries


We are showing results in a table from a MySQL database with checkboxes inside it. How can we delete many rows simultaneously if the user ticks 5 rows and clicks the delete button?


Solution

  • HTML:

    <form method="post">
    
        <input type="checkbox" name="id[]" value="123" /> Element 1 <br/>
        <input type="checkbox" name="id[]" value="5434" /> Element 2 <br/>
        <input type="checkbox" name="id[]" value="24" /> Element 3 <br/>
        <input type="checkbox" name="id[]" value="76" /> Element 4 <br/>
    
        <input type="submit" value="delete" />
    
    </form>
    

    PHP:

    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    
      $ids = array_walk($_POST['id'], 'intval');
      mysql_query('DELETE FROM table WHERE id IN (' . implode(',', $ids) . ')');
    
    }
    

    P.S. I want points !