I have an interface where the user adds, edit or delete products, once I save the changes I go to the products table, delete everything by ID and add the products that the user just selected, is this a bad way? how could I better thank you
-
1Why not use UPDATE?– MechCommented Sep 17, 2020 at 18:53
-
In the interface the user can edit delete and many things, but when he presses save, I don't know how to detect which products were saved or what was modified– wertyCommented Sep 17, 2020 at 18:58
-
I save the information in an array that contains all the products that the user lists, but then I don't know the differences between the array with the information and the table.– wertyCommented Sep 17, 2020 at 18:59
1 Answer
Yes! it's a bad way cause every time you perform two tasks instead of one. like if you want to update product you can update it directly using UPDATE query rather than delete and then insert
What will be the benefit of this UPDATE
- Query will be easier
- you can get result in single query
- if you have id it will be the same rather than changing every time cause you insert new record every time
Ex: think that you are making an e-commerce website a user visits your website and added the product in his/her cart but doesn't check out at that time. it saved it for later meanwhile you change the details of your product so you removed old id and inserted the new one.
now what's happens when user check his/her cart it will not show the product or some sort of error will generated as you have created your e-commerce platform so using update is far more better then delete-insert process for editing the product
Syntax:
UPDATE TABLE_NAME SET COLUMNS_NAME=NEW_VALUE [where clause]
Ex:
UPDATE PRODUCT SET product_name='HELLO WORLD' where id=1