Basic insert, view, edit, delete and update using PHP and Mysql Hi readers, today i going to post one of old topic in php and mysql, here i'm going to post Insert data through form, and fetch / view the data from database, and also Edit, Delete and Update with detailed explanation. Create PHP To Insert, Select, Update, Delete In MySQL Database Table For example, it is used for registration application, verification etc. Phptpoint.com is best website. Create PHP To Insert, Select, Update, Delete In MySQL Database Table Inside this post, we will learn about the various types of processing using a MySQL database from php programming language. Read this tutorial. Basic insert, view, edit, delete and update using PHP and Mysql Hi readers, today i going to post one of old topic in php and mysql, here i'm going to post Insert data through form, and fetch / view the data from database, and also Edit, Delete and Update with detailed explanation. In this tutorial we will show you how to add, edit and delete records using jQuery, Ajax, PHP and MySQL.In this way you can do any modification in MySQL database dynamically means without refreshing your page.In this you can insert new rows in database, edit existing row and update the row in. In this article, I will be presenting simple PHP & MySQL code to add, edit, delete and view data. This kind of system is also referred to CRUD (Create, Read, Update, Delete).

Active8 years, 7 months ago

I am working on a web app project and there is a rather large html form that needs to have its data stored in a table. The form and insert are already done but my client wants to be able to load the saved data back into the HTML form and be able to change it, again, this is no problem, but I came across a question when going to do the update, would it be appropriate to just keep the insert query and then delete the old row if it was an edit?

Basically, what already happens is when the form is submitted all of the data is put into a table using INSERT, I also have a flag called edit that contains the primary key ID if the data is for an existing field being updated. I can handle the update function two ways:

How to insert update and delete using php and mysql tutorial

a) Create an actual update query with all the fields/data set and use an if/else to decide whether to run the update or insert query.

How To Insert Update And Delete Using Php And Mysql

b) Do the insert every time but add a single line to DELETE WHERE row=editID after the insert is successful.

How To Insert Update Delete In Php Mysql

Since the Delete would only happen if the INSERT was successful I don't run the risk of deleting the data without inserting, thus losing the data, but since INSERT/DELETE is two queries, would it be less efficient than just using an if/else to decide whether to run an insert or update?

There is a second table that uses the auto-increment id as a foreign key, but this table has to be updated every time the form is submitted, so if I delete the row in table A, I will also be deleting the associated rows from table b. This seems like it would be bad programming practice, so I am leaning towards option a) anyway, but it is very tempting just to use the single line option. The DELETE would basically be as follows. Would this in fact be bad programming practice? Aside from conventions, are there any reasons why this is a 'never do that!' type of code?

linuxbuild
13.8k4 gold badges53 silver badges81 bronze badges
awestover89awestover89
8312 gold badges12 silver badges31 bronze badges

3 Answers

Whilst the INSERT/DELETE option would work perfectly well I'd recommend against it as:

  • Unless you bundle the INSERT/DELETEup into a single transaction, orbetter yet encapsulate theINSERT/DELETE up into a storedprocedure you do run the theoreticalrisk of accumulating duplicates. Ifyou use a SP or a transaction you'rejust effectively rewriting the UPDATEstatement which is obviouslyinefficient and moreover will giverise to a few WTF raised eyebrowslater by anyone maintaining yourcode.
  • Although it doesn't sound like anissue in your case you arepotentially impacting referentialintegrity should you need that. Furthermore you are loosing therather useful ability to easilyretrieve records in creation order.
  • Probably not a great consideration ona small application, but you aregoing to end up with a seriouslyfragmented database fairly quicklywhich will slow data retrieval.
CruachanCruachan
14.2k4 gold badges54 silver badges106 bronze badges

Update is only one round trip to the server, which is more efficient. Unless you have a reason that involves the possibility of bad data, always default to using an UPDATE.

Ken DownsKen Downs

Chapter 7 How To Insert Update And Delete Data

4,1131 gold badge18 silver badges19 bronze badges

It seems to me that doing the delete is pointless, if you run an update in MySql it will only update the record if it is different that what is stored already, is there some reason why you would need to do a delete instead. I usually use a case(switch) to catch update/delete calls from the user,

How To Insert Update And Delete Using Php And Mysql For Dummies

Stephen NielsenStephen Nielsen

Database Insert Update And Delete Method

Not the answer you're looking for? Browse other questions tagged mysqlsql-update or ask your own question.