PHP problem - delete and insert scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

PHP problem - delete and insert scripts

Post 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.
Last edited by KevinCB on Fri May 13, 2005 6:51 am, edited 4 times in total.
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post by Revan »

Shouldn't that be:

Code: Select all

$_POST["op"]
?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

skara your right but it is recomened to use quotes.
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

Post 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?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

tableName.fieldName
Post Reply