Jump to content

Deleting a record from a table


PaulRyan

Recommended Posts

I was wondering, what is the common practice for deleting a record from the database?

 

I can personally think of 2 ways I have done it in the past.

 

You send a delete query, whether the record exists or not.

 

Or you can first query the database, to see if the record exists, then if it does, delete it.

 

Just looking for some external insight into this :)

Link to comment
https://forums.phpfreaks.com/topic/276017-deleting-a-record-from-a-table/
Share on other sites

I personally always run a DELETE query, but I'm interested as to how others (better programmers?) do this.

 

When I need to let the user if the delete was a success, I check *_affected_rows(). If thats 0, the *_query() would still return as if it was a success. If affected rows returns 0, it means the delete didnt actually occur. Just the way I tried it. Mostly because I thought it makes sense, I'm no pro.

I too do it that way, when I want the user to see a response, but for personal things, I just expect the delete to run and work.

 

I used to select first, if it existed, delete it, if not, then display an error saying "You cannot delete what does not exist" or something of that nature.

*_affected_rows() might not mean it didn't occur because it didn't exist, could be because of an error or something.

 

It would only be heavier if the row exists, because you then have to delete the record.

 

I think there's a difference between something not existing and the query not working as expected, which is useful for the end user.

 

I might be thinking about this from too far outside the box if I'm honest, your method is logical and normally what I'd think of doing.

There's another option: not actually deleting the row at all, and instead marking it as deleted via a flag column.  Very useful if you want to remove data publically but keep it for your own internal records.  Our old SMF forums here did that.  Deleted posts/threads weren't actually deleted, but rather moved to a staff-only section.  It allowed us to keep a history of disruptive and/or abusive members.

  On 3/22/2013 at 2:20 PM, KevinM1 said:

There's another option: not actually deleting the row at all, and instead marking it as deleted via a flag column.  Very useful if you want to remove data publically but keep it for your own internal records.  Our old SMF forums here did that.  Deleted posts/threads weren't actually deleted, but rather moved to a staff-only section.  It allowed us to keep a history of disruptive and/or abusive members.

 

Well now there's a thought, think I might just do that from now on then. It's always good to keep a record of things.

I remember doing this some time ago, dunno why I don't do it anymore.

 

Thanks KevinM1.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.