I'd like to create a statement for my PHP function where I have an UserID
and an InteressenID
and update my userinteressen
table depending on what the user has checked. I don't want duplicates in that table.
I have found the INSERT ... ON DUPLICATE KEY UPDATE
in documentation, however, that doesn't work since in the table doesn't have an UNIQUE
row set.
Due working with MySQL, I don't have the MERGE
statement.
Since I receive the InteressenID
from my PHP function, there's no need for a JOIN
. I tried INSERT IGNORE
described here, but it returned Error #1064
For deleting, I was thinking about first setting the InteressenID
to 0 and delete them afterwards.
The tables:
User
ID | Name
---+--------
1 | "Agathe"
2 | "Bernd"
3 | "Charlie"
Interessen
ID | Name
---+------------
1 | "Cars"
2 | "Sport"
3 | "Languages"
userinteressen
User | Interessen
-----+-----------
1 | 2
1 | 3
2 | 2
My not working attempts:
INSERT INTO userinteressen VALUES('$id','$interesse')
SELECT *
WHERE userinteressen.User IS NULL AND userinteressen.Interesse IS NULL;
INSERT IGNORE INTO `userinteressen`
SET `User`= 1
`Interesse`= 2;
More Informations: I'm using PDO. The userinteressen-table was created by using the interface of phpmyadmin, thus I had no real idea, what the DB looked like.
I exported the database to get some insights. I'm new to databases and had mostly theory. Here are the statements created by the Engine:
--
-- Indices for Tabelle `userinteressen`
--
ALTER TABLE `userinteressen`
ADD KEY `User` (`User`,`Interesse`),
ADD KEY `Interesse` (`Interesse`);
--
-- Constraints of table `userinteressen`
--
ALTER TABLE `userinteressen`
ADD CONSTRAINT `userinteressen_ibfk_1` FOREIGN KEY (`User`) REFERENCES `user` (`UserID`),
ADD CONSTRAINT `userinteressen_ibfk_2` FOREIGN KEY (`Interesse`) REFERENCES `interessen` (`InteressenID`);
COMMIT;
Thanks!