+----+----------+-----------+
| id | gamertag | timestamp |
+----+----------+-----------+

So I have the table above I want to only insert a gamertag if it doesn't already exist. How could I do that?

$gamertag = "l RaH l";
mysql_query("INSERT INTO `gamertags`(`gamertag`)VALUES ('".$gamertag."')");

Accepted Answer

You should do both things: Create a unique index on column gamertag and use insert ignore.

The unique index prevents a duplicate row from being added.

The insert ignore prevents mysql from issuing a warning when the duplicate row is not added.

Written by awm
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki