Jump to content
Larry Ullman's Book Forums

Deleting A Page


Recommended Posts

I have created a page to update a page but can't figure out how to set up a page to delete a page. I was reading a book that said hyperlinks shouldn't be used to preform actions like deleting. The only refererence material I have for deleting information is from the PHP6 and MYSQL5 book. I was hoping to recieve some insight regarding this subject.

Thanks

Link to comment
Share on other sites

First off, there are two schools of thought on deleting data from a database:

 

1) It's okay to do.

2) Don't delete the data, but instead create an active/inactive flag, and change that flag.

 

I tend to prefer the second option. In general, it's best to avoid deleting data from a database. By using a flag, you can simply test for whether the flag is active or not, and if it isn't, don't display the data.

 

With that said, the easiest way (as is the case in Larry's PHP 6 & MySQL 5 book) is to send the ID of the record to be deleted to a script used for deleting, and from there, ask the user whether they're sure they want to delete the record. If they say yes, then with the ID you have, you can either delete the corresponding record or you can toggle the activity flag, depending on whether you go with 1 or 2 above.

 

Does that answer your question? If you're going for something else, then you'll need to provide some more info about your specific example.

  • Upvote 1
Link to comment
Share on other sites

I am working on a site where the data will change and I update hopefully on a regular basis as they sell items. I want to create something that allows them to go in and delete or flag it doesn't matter. I just tried using the example from the book I mentioned earlier and can't get it to work. do you have any samples of code that show an example of what your are refering to.

Thanks

Link to comment
Share on other sites

I don't have the book in front of me, but going off what I remember of Larry's example in his PHP 6 & MySQL 5 book, I'd doing something like the following:

 

Have some sort of admin screen that lists all (or just some of) the products. On that screen, in an HTML table, have a Delete link for each item. The Delete links would have to be coded using something like the following, which would most likely be performed from a PHP while loop after grabbing all the necessary item info from the database:

 

echo '<td><a href="deleteitem.php?id=' . $row['id'] . '">Delete</a></td>';

 

And then you'd have to write the deleteitem PHP script, which would receive the ID value via the GET method, and use that to delete/flag the appropriate item. The code might be something like the following:

 

$id = $_GET['id'];

$q = "DELETE FROM table-name WHERE id=$id";

 

And then you'd execute and verify the query, etc. Or if you wanted to adjust a flag, your query might look like the following:

 

$q = "UPDATE table-name SET active_flag='inactive' WHERE id=$id";

 

Anyway, I hope that helps. Also, please keep in mind that this example is greatly simplified. You'd need to write the rest of the scripts, and also, you need to consider security issues, so that random people can't just delete whatever.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...