Delete records where checkbox are ticked on a list

Posted on 2006-04-10
Medium Priority
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
  • 3
  • 2
LVL 17

Expert Comment

ID: 16423231
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

ID: 16423276
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

BogoJoker earned 1000 total points
ID: 16423310
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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 16423436
<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

ID: 16427480
Joe...you 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

ID: 16428196
Sure thing. =)
Joe P

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 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