How To Insert Update And Delete Using Php And Mysql
- How To Insert Update Delete In Php Mysql
- How To Insert Update Delete Record Using Php And Mysql
- Database Insert Update And Delete Method
- Insert Update And Delete Video In Hindi
- How To Insert Update And Delete Using Php And Mysql For Dummies
- How To Insert Update And Delete Using Php And Mysql 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. UPDATE essentially is 'insert a new one and delete the old one,' except that 1) it's an atomic operation, 2) it keeps the same ID 3) it's the way MySQL natively understands this and 4) it's the way everyone else does it. 3 and 4 are both important. MySQL will be optimized best for doing things the standard way. In this article we will learn how to insert, edit, update and delete records from the database using PHP. ('Cannot select database. Mysql_error());?> view.php.
i need in one query use select, insert, delete and update.
(I need copy data from old table in to new, then delete old, and update another).
Insert and select (copy function I was able to, but now i have problem)
I have this query:
Thanks
user319854user3198544 Answers
You can't do it all in one query, but you can do it all in one transaction if you are using a transactional store engine (like InnoDB). This might be what you want, but it's hard to tell only using the information you provided in your question.
Ike WalkerIke WalkerMySQL
does not support MERGE
, so you'll have to do it in two queries:
, provided you have PRIMARY KEY (id)
in both tables.
In one query i dont think its possible.
You can try writing a Stored Procedure and using Triggers you may achieve that
zodzodYou can't combine Select/Update/etc into one query. You will have to write separate queries for each operation you intend to complete.
How To Insert Update Delete In Php Mysql
pinkfloydx33pinkfloydx33How To Insert Update Delete Record Using Php And Mysql
Not the answer you're looking for? Browse other questions tagged mysql or ask your own question.
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:
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.
b) Do the insert every time but add a single line to DELETE WHERE row=editID after the insert is successful.
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?
linuxbuild3 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.
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 DownsIt 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,