Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checkboxes, arrays and delete statement

    Hi

    here is the basic code used to display data (texts titles). I'd like to allow multiple deletes thank to checkboxes. I should get all the pks in an array and delete them all. I am kinda stuck with the array part...

    PHP Code:
    $sql = @mysql_query("SELECT pk, title, FROM texts ORDER BY pk DESC"); 
    if (!$sql) { 
       die('<p>Error performing query: ' . mysql_error() . 
           '</p>'); 


    while ( $row = mysql_fetch_array($sql) ) { 
    ?> 
    <p> 
    <input type="checkbox" value="<?=$row['pk']?>" name="pk"> 
    <a href="display.php?text=<?=$row['pk']?>"><?=$row['title']?></a> 
    </p>
    any help out there ?


  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why do you need the array ?


    if you post the form (with a few boxes chackt) and you do a

    $_POST['pk']

    then you should get them printed as a commadelimited list.

    So if you use


    "delete from table where pk In (". $_POST['pk'] . ")"

    then that shoudl do the trick.
    But i alsays add the pk to the name and then loop through the $_POST to build the collection.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by raf

    But i alsays add the pk to the name and then loop through the $_POST to build the collection.
    an example to post?

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you should change your form to this:

    PHP Code:
    while ( $row = mysql_fetch_array($sql) ) { 
    ?> 
    <p> 
    <input type="checkbox" value="<?=$row['pk']?>" name="[b]pk[][/b]"> 
    <a href="display.php?text=<?=$row['pk']?>"><?=$row['title']?></a> 
    </p>
    And then you can use:
    PHP Code:
    $commaDelimited implode", "$_POST["pk"] );
    $res mysql_query("delete from table where pk In ( $commaDelimited )"); 

  • #5
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *cross posts COL let me give it a try.

    For some reasons, it only deletes one item at a time...?

    form:
    PHP Code:
    <form action="editDeleteUserCP.php" method="POST">
    <?php
    while ( $row mysql_fetch_array($result) ) {
    ?>
    <p>
    <input type="checkbox" value="<?=$row['pk']?>" name="textID">
    <a href="editUserCP.php?text=<?=$row['pk']?>"><?=$row['title']?></a>
    </p> 
    <?php
    }
    ?>
    <input type="submit" value="delete" />
    </form>
    deletion script:
    PHP Code:
    $textID $_POST['textID'];

    $sql = @mysql_query("DELETE FROM links WHERE pk IN ("$textID ")"); 
     if (!
    $sql) { 
       die(
    '<p>Error performing query: ' mysql_error() . 
           
    '</p>'); 
     }  
     
    header("Location:indexUserCP.php");
    exit; 

  • #6
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok it works great

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by jeskel
    *cross posts COL let me give it a try.

    For some reasons, it only deletes one item at a time...?

    form:
    PHP Code:
    <form action="editDeleteUserCP.php" method="POST">
    <?php
    while ( $row mysql_fetch_array($result) ) {
    ?>
    <p>
    <input type="checkbox" value="<?=$row['pk']?>" name="textID">
    <a href="editUserCP.php?text=<?=$row['pk']?>"><?=$row['title']?></a>
    </p> 
    <?php
    }
    ?>
    <input type="submit" value="delete" />
    </form>
    deletion script:
    PHP Code:
    $textID $_POST['textID'];

    $sql = @mysql_query("DELETE FROM links WHERE pk IN ("$textID ")"); 
     if (!
    $sql) { 
       die(
    '<p>Error performing query: ' mysql_error() . 
           
    '</p>'); 
     }  
     
    header("Location:indexUserCP.php");
    exit; 
    Did you notice my post?!

  • #8
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by jeskel
    ok it works great
    was about your post


  •  

    Posting Permissions

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