Delete records where checkbox are ticked on a list


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>';
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
stevegingellAuthor Commented:
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?
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

blue_hunterTechnical ConsultantCommented:
<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>
stevegingellAuthor Commented: 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.
Sure thing. =)
Joe P
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.