Page 1 of 1

PHP problem - delete and insert scripts

Posted: Wed May 11, 2005 2:37 pm
by KevinCB
I'm currently having problems with the delete and insert scripts for my site.

The delete script finds the records entered and displays them in a combo box perfectly fine, then when I press the button to tell it to delete it says that it has deleted the record, but when looking at the tables in the database, the values are still in them.

Delete Script

Code: Select all

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 id, concat_ws(', ', item_name) as display_name from store_items";
   $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)) {
          $id = $recs['id'];
          $display_name = stripslashes($recs['display_name']);

          $display_block .= "<option value=\"$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 id = '.$id;
    mysql_query($del_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>";
}
The insert image script, that doesn't work either. I created a store_images table in the database with the SQL code supplied, and created an upload.php script with the changes to my database, and also made changes to the form on the other page. All the previous data inserts into the other tables OK, but nothing seems to get inserted into the store_images table. It's as if it isn't running the upload.php script at all.

As for the add image script I've been using the following guide on this site:

http://www.evolt.org/article/Storage_an ... /20/27237/

The only thing I changed in the upload.php script was:

The database table containg the image info to store_images
The database connection and database name

And the img.php file is the same as the one from the site, no changed were made for this.

Posted: Wed May 11, 2005 5:11 pm
by Revan
Shouldn't that be:

Code: Select all

$_POST["op"]
?

Posted: Wed May 11, 2005 6:23 pm
by Skara
Actually, arrays can have keys without quotes.

I'm pretty sure that $ar[foo] isn't equivalent to $ar['foo'] but can be accessed that way. O.o (aka I have no clue.)

Posted: Thu May 12, 2005 5:48 am
by phpScott
skara your right but it is recomened to use quotes.

Posted: Fri May 13, 2005 4:31 am
by KevinCB
I've now got some of it sorted, and am still having problems.

Tables

store_items - store_books

Fields

id - item_id

This is how the tables are linked, and as you can see from above the id in the tables are different from each other, they contain the same value, but are different names. So it gives me this message
Unknown column 'id' in 'where clause'
What I need to know is, is it possible to tell the script to use the id from another table?

Posted: Fri May 13, 2005 7:21 am
by phpScott
tableName.fieldName