Page 1 of 1

[SOLVED]delete script

Posted: Wed May 18, 2005 5:34 am
by KevinCB
I created this delete script the other week, which worked fine, but I made some minor changes to the mysql select statement, because I only wanted to delete certain values from the tables, and now it's not deleting anything :(

The script creates the form that is on the page, but it just seems to miss out the delete queries altogether, could someone possibly check it for me.

Thanks

Kevin

Code: Select all

<?php
if ($_POST['op'] != "delete") {
   //haven't seen the form, so show it
   $display_block = "<h1>Select a Book</h1>";
   //get parts of records
   $get_list = "SELECT cat_id, concat_ws(', ', item_name) AS display_name FROM store_items
        RIGHT JOIN store_categories ON store_items.cat_id = store_categories.id
         WHERE store_categories.id = 1";
   $get_list_res = mysql_query($get_list)  or die(mysql_error());

   if (mysql_num_rows($get_list_res) < 1) {
   //no records
   $display_block .= "<p><em>Sorry, no records to select!</em></p>";

   } else {
       //has records, so get results and print in a form
       $display_block .= "
       <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
       <p><strong>Select a Record to Delete:</strong></p><br>
       <select name=\"sel_id\">
       <option value=\"\">-- Select One --</option>";

       while ($recs = mysql_fetch_array($get_list_res)) {
          $cat_id = $recs[cat_id];
          $display_name = stripslashes($recs['display_name']);

          $display_block .= "<option value=\"$cat_id\">
               $display_name</option>";
       }
       $display_block .= "
       </select>
       <input type=\"hidden\" name=\"op\" value=\"delete\">

       <p><input type=\"submit\" name=\"submit\" value=\"Delete Selected Entry\"></p>
       </form>";
   }

} else if ($_POST['op'] == "delete") {

    //check for required fields
      if ($_POST['sel_id'] == "") {
        header("Location: delbook.php");
        exit;
    }

    //issue queries

    $del_items = 'DELETE FROM store_items WHERE id = '.$_POST['sel_id'];
    mysql_query($del_items)  or die(mysql_error());

    $del_books = 'DELETE FROM store_books WHERE item_id = '.$_POST['sel_id'];
    mysql_query($del_books)  or die(mysql_error());

    $flush_items = 'FLUSH TABLE `store_items`';
    mysql_query($flush_items)  or die(mysql_error());

    $flush_books = 'FLUSH TABLE `store_books`';
    mysql_query($flush_books)  or die(mysql_error());

    $display_block = "<h1>Record(s) Deleted</h1>
    <p>Would you like to
    <a href=\"$_SERVER[PHP_SELF]\">delete another</a></p>";
}
?>
<html>
<head>
<title>Delete a Book</title>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>

Posted: Wed May 18, 2005 5:49 am
by phpScott
are you sure that

Code: Select all

$_POST['op'] == "delete"
is true.

that $_POST['op'] is equal to "delete";

before you get to that stage just echo it out to make sure you are getting the value you are expecting

Posted: Fri May 20, 2005 6:25 am
by KevinCB
I have echoed out $_POST['op'] and it is equal to delete, so I still have no idea why it isn't running the delete queries.

Posted: Fri May 20, 2005 7:44 am
by phpScott
do you get your $display_block printed to the screen?

just trying to narrow where the problem might be.

Posted: Fri May 20, 2005 8:01 am
by KevinCB
OK, it's working now, weird thing is I was adding stuff to some of my other scripts, and just added another delete query into the code, and now its working, seriously weird :o