SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    Non-Member
    Join Date
    Aug 2007
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete Records using Checkboxes

    Hi,

    How do I display all records from the database, along with a check box?
    Upon a submit button being clicked, how do I delete selected records?

  2. #2
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the numeric key for each record in the value attribute of the checkbox. Name the checkboxes as a group using array syntax and then loop through the array on post and delete each record that has an id in the array.

    Code:
    <input type="checkbox" name="delgroup[0]" value="215" />
    <input type="checkbox" name="delgroup[1]" value="217" />
    <!-- etc. etc. -->
    PHP Code:
    if(!empty($_POST['delgroup']) && is_array($_POST['delgroup']))
    {
       
    //connect to db blah blah blah
       
    for($i 0;$i count($_POST['delgroup']);$i++)
       {
           
    $sql 'DELETE FROM whatevertable WHERE id='.intval($_POST['delgroup'][$i]);
           
    mysql_query($sql);
       }


  3. #3
    Non-Member
    Join Date
    Aug 2007
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete Records using Checkboxes

    Hammer65,

    I found the following code within the forums, how do I merge you code into it, resulting in records being deleted?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my_db", $con);
    
    $result = mysql_query("SELECT * FROM person");
    
    while($row = mysql_fetch_array($result))
    {
      echo $row['FirstName'];
      echo $row['LastName'];
    }
    
    mysql_close($con);
    ?>
    Many Thanks in Advance!!! - Much Appreciated!!!

  4. #4
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This assumes that each record has a unique integer id (which it should). Put the following within the form code.

    PHP Code:
    $con mysql_connect("localhost","peter","abc123");
    if (!
    $con)
    {
       
    // in development okay not production
      
    die('Could not connect: ' mysql_error());
      
    // for production
      // echo('<h2>Sorry!</h2><p>We are having technical difficulties</p>');
    }
    else  
    {

        
    mysql_select_db("my_db"$con);
        
        if(
    $result mysql_query("SELECT id,concat(FirstName,' ',LastName) as name FROM person"))
        {
            
    $i 0;
            if(
    mysql_num_rows($result) > 0)
            {
                while(
    $row mysql_fetch_array($result))
                {
                  echo 
    '<label>';
                  echo 
    '<input type="checkbox" name="delgroup['.$i.']" value="'.$row['FirstName'].'" />'."\n";
                  echo 
    $row['name'].'</label>';
                  
    $i++;
                }
            }
            else
            {
                echo 
    '<h2>Sorry!</h2><p>No Records Found.</p>';
            }
        }
        else
        {
             
    // in development okay not production
              
    die('Query failed: ' mysql_error());
              
    // for production
              // echo('<h2>Sorry!</h2><p>We are having technical difficulties</p>');
        
    }
        
    mysql_close($con);

    Use the previous code for the submission.

  5. #5
    Non-Member
    Join Date
    Aug 2007
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete Records using Checkboxes

    Use the previous code for the submission.
    How do I go about doing that?

    Many Thanks,

  6. #6
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the form and the checkbox code on one php page and the submission code on another. Set the form to post to the page with the submission code on it. Look for a few PHP tutorials on this sort of thing. I can't write all the code for you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •