In your code, you're aiming to update rows in the set_colors table if they already exist. Otherwise, insert new rows. However, you're utilizing the IF EXISTS conditional within the query, which doesn't align with the preferred way to handle such scenarios.
To perform the desired update or insert operation based on field presence, consider using the INSERT ... ON DUPLICATE KEY UPDATE syntax:
INSERT INTO <table name> (field1, field2, field3, ...) VALUES ('value1', 'value2', 'value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ...
With this syntax:
The following query demonstrates the usage:
INSERT INTO set_colors (school_art_id, baseimage_id, sub_folder, layer) VALUES ('1', '2', 'test-folder', '10') ON DUPLICATE KEY UPDATE baseimage_id='2', sub_folder='updated-folder', layer='15'
The above is the detailed content of How to Update or Insert Rows in MySQL Based on Field Presence?. For more information, please follow other related articles on the PHP Chinese website!