Delete records where checkbox are ticked on a list

Posted on 2006-04-10
Last Modified: 2008-03-10

I used this code that someone had asked earlier. It works perfectly, except that after i hit delete, nothing happens. in other words the view is not refreshed. Only when i go back to the page or refresh i can see the items have been deleted. Here is the code that i used:

<?php if(isset($_SESSION["username"]))
     // start -> delete any valid checked rows
     if ( isset ( $_POST['option'] ) )
          $temp = array ();
          foreach ( $_POST['option'] AS $id )
               $id = intval ( $id );
               if ( $id > 0 )
                    $temp[] = 'listingid = ' . $id;
          if ( ! empty ( $temp ) )
               $sql = "DELETE FROM listings WHERE " . implode ( ' OR ', $temp );
               mysql_query ( $sql );
               unset ( $id );
          unset ( $temp );
     // end -> delete any valid checked rows
// Table header.
echo' <form name="form1" method="post" action='.$_SERVER["PHP_SELF"];
echo '><table align="center" cellspacing="0" cellpadding="5" width="100%" class="gridtable">
<tr class="header" align="left">
      <td align="center" class="editanddelete" width="5%" valign="top">Edit</td>
      <td align="center" class="editanddelete" width="5%" valign="top">Delete</td>
      <td align="left" width="30%" valign="top"><a href="' . $link1 . '" class="gridheader">Title</a>' . $sortimg1 . '</td>
      <td align="left" width="40%" valign="top"><a href="' . $link2 . '" class="gridheader">Description</a>' . $sortimg2 . '</td>
      <td align="left" width="15%" valign="top"><a href="' . $link3 . '" class="gridheader">Price</a>' . $sortimg3 . '</td>
      <td align="left" width="5%" valign="top"><a href="' . $link4 . '" class="gridheader">Display</a>' . $sortimg4 . '</td>
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      echo '<tr>
            td align="left">' . $row[edit] . '</td>
            <td align="center"><input type="checkbox" name="option[]" value="' . intval( $row['listingid'] ) . '"</td>
            <td align="left">' . $row['title'] . '</td>
            <td align="left">' . $row['description'] . '</td>
            <td align="left">' . $row['price'] . '</td>
            <td align="left">' . $row['display'] . '</td>
            echo '</table><p><input type="submit" value="Delete Checked" class="button" /></p></form>'; // Close the table.

            mysql_free_result ($result); // Free up the resources.      
      } else { // If there are no registered users.
            echo '<span class="darkbluebig">There are currently no listings.</span>';
      mysql_close(); // Close the database connection.
                        else {
                        echo '<span class="content"><br><br><br><br><br><br><p align="center">You are not logged in.<br><br>To login click <a href="../login.php">here</a>.</p></span>';
Question by:stevegingell
    LVL 17

    Expert Comment

    Hi stevegingell,

    Am I missing where you define $result to do: (I assume you left out some php where you setup your connection)
    $row = mysql_fetch_array($result, MYSQL_ASSOC)

    It is important to know when $result was defined,
    if it was before you DELETED the rows then there is your problem, it still has the deleted rows.

    Joe P

    Author Comment

    Hi Joe,

    Yes I have defined $result earlier in my code..heres a snippet from where its coming:

          $query = "SELECT customers.*, Listings.* FROM customers INNER JOIN listings ON customers.customerid =  listings.customerid WHERE customers.customerid = " .$_SESSION['customerid']. " ORDER BY listings.$order_by LIMIT $start, $display";
          $result = @mysql_query ($query); // Run the query.
          $num = mysql_num_rows ($result); // How many listings are there?
    LVL 17

    Accepted Solution

    Like I said before make sure that is AFTER your:
         // end -> delete any valid checked rows

    If it was before that point then it will still have the rows which you will "later" delete.
    So make sure your query is AFTER that point and it should work just fine.

    Joe P
    LVL 9

    Expert Comment

    <td align="center"><input type="checkbox" name="option[]" value="' . intval( $row['listingid'] ) . '"</td>  <--------
     you left out somethings in the row, the checkbox control is not form properly.

    it should be like below --->
    <td align="center"><input type="checkbox" name="option[]" value="' . intval( $row['listingid'] ) . '" /></td>

    Author Comment

    by:stevegingell were right on..thanks.

     blue_hunter you are right..the tag was missing right at the end..but it didnt seem to make a difference even after i added it.
    LVL 17

    Expert Comment

    Sure thing. =)
    Joe P

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now