I have a table that links 2 other tables together.
I have a list of checkboxes, where when one is checked, it sends the value to the server via ajax.
What I want to do is if the values are not in the database, insert them (checked) or if they are there, delete them (unchecked)
Is there a way to do it without writing several queries? I know its not to hard with an insert/update, but what about delete?
There are three ways you can perform an “insert if not exists” query in MySQL: Using the INSERT IGNORE statement. Using the ON DUPLICATE KEY UPDATE clause. Or using the REPLACE statement.
Often you have the situation that you need to check if an table entry exists, before you can make an update. If it does not exist, you have to do an insert first. Unfortunately, this the 'ON DUPLICATE KEY' statement only works on PRIMARY KEY and UNIQUE columns.
You can allways delete and, if affected rows is 0, then insert. Easy, simple and it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With