Checkbox Delete (MySQL)

Discussion in 'PHP' started by bobby9101, Dec 14, 2006.

  1. #1
    say i have 5 emails stored in a DB
    I set up it to echo out all the emails, and have the checkbox, but how do I get the check boxes to delete the selected emails?
     
    bobby9101, Dec 14, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Here an example.

    
    
    if (is_array($_POST['email']))
    {
         mysql_query("DELETE FROM mails WHERE mailid IN(". implode(', ' array_map('intval', $_POST['email'])) .")");
    }
    
    PHP:
    And put something like this in the loop when receiving the mails from the database.

    
    <input type="checkbox" name="email[]" value="<?php echo $row['emailid']; ?>" />
    
    HTML:
     
    nico_swd, Dec 14, 2006 IP
  3. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I did not undersood your question.
    Will you plz. reframe it.. :)
     
    weknowtheworld, Dec 14, 2006 IP
  4. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use a form with the checkboxes and post it to nico_swd's PHP code.

    What part did you not understand weknowtheworld?
     
    Krownet, Dec 14, 2006 IP
  5. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I get: Parse error: syntax error, unexpected T_STRING
     
    bobby9101, Dec 14, 2006 IP
  6. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    if (is_array($_POST['email'])) {
         mysql_query("DELETE FROM mails WHERE mailid IN(". implode(', ' . array_map('intval', $_POST['email'])) .")");
    }
    
    PHP:
    Here is nico_swd's code fixed up.. Pesky "."s

    haha

    Cheers,
     
    Krownet, Dec 14, 2006 IP
  7. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I got it to work using
    if (isset($_POST[email])) {
    foreach($_POST as $k => $v){
    $$k = $v;
    }
    foreach($email as $v){
        mysql_query("DELETE FROM mail where mailid='$v'");
    }  
    }
    PHP:
    I couldn't get it to work your way :(
    Can anyone explain the above code? I looked up foreach on php.net, but it made no sense to me.
    WHy do i need $k (i am assuming i can use any other variable, but still it seems roundabout to do it that way)
     
    bobby9101, Dec 14, 2006 IP
  8. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If you want to do it that way, it should look more like this:

    
    
    if (isset($_POST['email'])) {
        foreach($_POST['email'] as $key => $value){
            mysql_query("DELETE FROM mail where mailid='" . $value . "'");
        } 
    }
    
    
    PHP:
    Basically what will happen: Foreach looks at an array of objects and goes through each one. The "$key" is the value in the square brackets ([], usually something like $email[0] $email[1])... it identifies the current value and position of the search. The "$value" is the data (so for your checkbox, it would be the ID) .. this is what we want.

    Foreach will run through each object in the array and return it as variables.. this case $key and $value.

    To delete the ID just run
    mysql_query("DELETE FROM mail WHERE mailid='" . $value . "'");
    PHP:
    And it will delete that ID on the current passthrough.

    'mail' is the table name, 'mailid' is the match we're going to perform.

    Hope this helps.

    Cheers,


    (btw the above will save a few lines of code, and may be a little easier to understand).
     
    Krownet, Dec 14, 2006 IP
    bobby9101 likes this.
  9. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thank you very much.
    Rep added
    which was is faster, the array, or the foreach?
     
    bobby9101, Dec 14, 2006 IP
  10. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #10
    oh, and are $value and $key like global variables?
     
    bobby9101, Dec 14, 2006 IP
  11. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The array basically looks like this
    $email = array (
        key1 => value1,
        key2 => value2
    );
    PHP:
    $email[key1] = value1
    &
    $email[key2] = value2
    Code (markup):
    When running foreach ($email as $key => $value) this happens:

    1st pass through
    $key is set as key1
    $value is set as value1
    
    2nd pass through
    $key is set as key2
    $value is set as value1
    Code (markup):
    The thing with the foreach vs nico's code, is that you're running a query every loop through. So if you have 5 things to delete, you have to run 5 separate queries. This can slow down execution time (maybe not as important in an admin panel, or personal project... but in a live environment, the less queries the better). We're taking 2/100ths of a second, so it probably wont be noticed.




    Edit, something that may help is to create an array in php, and then run print_r($createdArray); where $createdArray is your array value. This will let you see it visually.
     
    Krownet, Dec 14, 2006 IP
  12. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ok, i tried nico's method (well your update to it) and got: Warning: implode() [function.implode]: Argument to implode must be an array. in...
     
    bobby9101, Dec 14, 2006 IP
  13. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Oh.. woops.

    
    if (is_array($_POST['email'])) {
         mysql_query("DELETE FROM mails WHERE mailid IN(". implode(', ', array_map('intval', $_POST['email'])) .")");
    }
    
    PHP:
    Sorry, that should be a comma not a period :p

    Cheers,
     
    Krownet, Dec 14, 2006 IP
    nico_swd likes this.
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    Lol, sorry. My bad... I didn't test it.
     
    nico_swd, Dec 14, 2006 IP
  15. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #15
    thanks it works
     
    bobby9101, Dec 14, 2006 IP