Link to home
Start Free TrialLog in
Avatar of stevegingell
stevegingell

asked on

Delete records where checkbox are ticked on a list

Hi,

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>
</tr>
';
            
// 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>
      </tr>
      ';
}
            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>';
            }
Avatar of BogoJoker
BogoJoker

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
Avatar of stevegingell

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blue_hunter
<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>
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.
Sure thing. =)
Joe P