I have MySql Table For Tags Like This:
| id | name |
| 1 | Linux |
| 2 | Windows |
| 3 | Unix |
| 4 | Dos |
| 5 | FreeBSD |
In News Page User add New Tags Like This:
Linux,OpenBSD,Test
Now, Linux
Previously added, So I need To add ONLY
OpenBSD and Test Into Tags Table. My Mean Is: how to INSERT
tags if User tags is New?!
UNIQUE
constraint on the name
column.Use INSERT IGNORE
so that inserting a column where name
already exists simply skips that row. eg:
INSERT IGNORE INTO tags (name) VALUES ('Linux'), ('OpenBSD'), ('Test');
linux
vs Linux
] you either need to make sure that you are using a case-insensitive coallation in your database like utf8_latin1_ci
[ci
stands for case-insensitive] or be sure to use strtolower()
on your data prior to inserting it.Like Jay suggested, normalise the data, then make the column UNIQUE
, and use something like INSERT IGNORE INTO yourTags(name) VALUES(?),(?),(?)
(and send in the three values as parameters). I'm also assuming that the ID column is AUTO INCREMENT
.