Results 1 to 10 of 10

Thread: checklist to remove from db

  1. #1
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default checklist to remove from db

    so i'm in the process of making a cms for some mods to be able to remove certain rows from a db.
    the list of rows displays correctly, and i got the checkbox idea for each row from this thread,
    http://www.dynamicdrive.com/forums/s...ad.php?t=43923

    and changed everything appropriately(or so i think) but when the delete button is pushed absolutely nothing happens.

    here's what the list looks like:
    PHP Code:
           <?php
           $query 
    "SELECT * 
                    FROM store 
                    ORDER BY TIME DESC"
    ;
           
    $result mysql_query($query$connection);
           if (!
    $result) {
                die(
    "Database query failed: " mysql_error());
           }
          echo 
    '<br /><b><u>LATEST UPLOADS:</u></b><br /><br />';
          echo 
    '<table border=1 align=center cellpadding=5>'
          echo 
    '<tr><th>URL OF UPLOAD</th><th>FROM IP</th><th>TIME</th>';
          echo 
    '<th><input name="delete" type="submit" id="delete" value="Delete" method="POST"></th></tr>';
           while (
    $row mysql_fetch_array($result))
           {
            
    $link "../".$row["url"];
                 echo 
    "<tr>";
                    echo 
    "<td><a href='{$link}'>{$row["url"]}</a></td>";
                    echo 
    "<td>{$row["IP"]}</td>";
                    echo 
    "<td>{$row["TIME"]}</td>";
                    echo 
    "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"{$row["id"]}\" /></td>";
                echo 
    '</tr>';     
           }
           if(isset(
    $_POST['delete']))
           {
             if (
    count($_POST['checkbox']) > 0) {
       foreach (
    $_POST['checkbox'] as $del_id) {
        
    $sql "DELETE FROM store WHERE id=' " $del_id " ' ";
        
    $result mysql_query($sql) or die(mysql_error()); 
        if(
    mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
                }
            }
           }
           
    ?>
    my code is slightly different than the link above in that i'm echoing my table in php.

    i see that he is making a $count variable at the top but it doesn't look like it's used in deleting so i don't think i need this. i may be completely wrong lol

    but anyway nothing happens on pressing the delete button, any ideas why?

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    You don't have any open and closing form tags. You can't use the method=POST in the submit button tag. It doesn't do anything cause the page doesn't think you submitted the form cause there isn't any "form" on the page.

  3. #3
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    oh wow good call. i put form tags around the entire thing, now the page refreshes when the button is pushed but nothing is deleted, whether i have some checked or not. hrmmm. something must be wrong with the delete function...

  4. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Post your current code.

  5. #5
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    PHP Code:
           <form>
           <?php
           $query 
    "SELECT * 
                    FROM store 
                    ORDER BY TIME DESC"
    ;
           
    $result mysql_query($query$connection);
           if (!
    $result) {
                die(
    "Database query failed: " mysql_error());
           }
          echo 
    '<br /><b><u>LATEST UPLOADS:</u></b><br /><br />';
          echo 
    '<table border=1 align=center cellpadding=5>'
          echo 
    '<tr><th>URL OF UPLOAD</th><th>FROM IP</th><th>TIME</th>';
          echo 
    '<th><input name="delete" type="submit" id="delete" value="Delete" method="POST"></th></tr>';
           while (
    $row mysql_fetch_array($result))
           {
            
    $link "../".$row["url"];
                 echo 
    "<tr>";
                    echo 
    "<td><a href='{$link}'>{$row["url"]}</a></td>";
                    echo 
    "<td>{$row["IP"]}</td>";
                    echo 
    "<td>{$row["TIME"]}</td>";
                    echo 
    "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"{$row["id"]}\" /></td>";
                echo 
    '</tr>';     
           }
           if(isset(
    $_POST['delete']))
           {
             if (
    count($_POST['checkbox']) > 0) {
       foreach (
    $_POST['checkbox'] as $del_id) {
        
    $sql "DELETE FROM store WHERE id=' " $del_id " ' ";
        
    $result mysql_query($sql) or die(mysql_error()); 
        if(
    mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
                }
            }
           }
           
    ?>
           </form>
    i've gotta fiddle with the last chunk of code, somethings not right there since nothings being deleted.

  6. #6
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Have you never used a form before? Take the method=POST out of the submit button. The top form tag should be like this
    Code:
    <form action="" method="post">

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    before you go too much further with this, be advised that you should NEVER have a form, like the one you posted, that allows the user to arbitrarily delete things from your database. You need to design this so it passes the request through a process that validates everything.

    (Adding that functionality on later, "once you get it working," doesn't work.)

  8. #8
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    ah man i'm derping hard. i have i have, just being an idiot. that worked. a strange bug though. after clicking delete it appears as if nothing happens until i refresh or visit the page again, then it shows that the entries have been deleted. it also doesn't echo the:
    "selected data rows deleted" that's supposed to come up.

    traq i should create a validation process even when it's just checkboxes for deleting entries? i can't imagine how there could be any harm but i'll take your word for it. but now i'm thinking about it and wondering what is there to validate? like to check in case it has already been deleted or something?

  9. #9
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Perhaps it's something as simple as an undeclared $connection.

    mysql_query($sql)

    as compared to

    mysql_query($query, $connection)

    Hope that helps.

  10. #10
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    gadgetguy changing
    PHP Code:
    mysql_query($sql
    to
    PHP Code:
    mysql_query($query$connection
    just queries the db again instead of performing what's set to $sql. ($sql, $connection) isn't a bad idea for more appropriate code though, although i think the connection is assumed at that point since it works w/o it. but still good practice.

    i'm also trying to use unlink on each selected row, since each row will have a url to a file. still figuring that part out hehe.
    Last edited by baconDelta; 01-29-2012 at 06:16 AM.

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
  •