I need to insert strings (unicode words) from php array into mysql table but I need no duplicate values In php can't check duplicates because the single words come by exploding text from various sources and some words can be the same as previous inserted
For each word I need also to insert their ASCII version in the same column but only if unicode word is different to ASCII converted words
In mysql I have one table ´words´ with two columns:
´id´ PRIMARY and ´word´ UNIQUE (utf8mb4_general_ci)
$sql = "
INSERT INTO ´words´ (´word´) VALUES ('$word')
ON DUPLICATE KEY UPDATE
SET ´word´ = '$word'
";
$query->execute();
//ex: if $word = "peter" and $word_ascii = "peter" do not insert $word_ascii
//but if $word = "julià" and $word_ascii = "julia" inert also $word_ascii
if($word != $word_ascii){
$sql = "
INSERT INTO ´words´ (´word´) VALUES ('$word_ascii')
ON DUPLICATE KEY UPDATE
SET ´word´ = '$word_ascii'
";
$query->execute();
}
But in this mode when I try to insert $word_ascii = "julia" that overwrite "julià" which I have already inserted before (due to UNIQUE index and ON DUPLICATE KEY UPDATE)
If I quit the UNIQUE index then have a lot of duplicate entries.
I have tried:
INSERT IGNORE (with and without UNIQUE),
REPLACE ... but still the same problem.
I have tried also to adapt IF NOT EXIST from this old answer : How to 'insert if not exists' in MySQL?
INSERT INTO ´words´ (´word´) VALUES ('$word')
WHERE NOT EXISTS (SELECT * FROM ´words´ WHERE ´word´ = '$word' LIMIT 1);
But unsuccessful. I got a syntax error.
Any help are welcome