Jump to content

Deleting a record from a table


Go to solution Solved by DaveyK,

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

  • Solution

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.