0

Here's the particular area im having an issue with

mysql_query("INSERT IGNORE INTO storeip (ip)
VALUES ('$ip')");

when testing this it keeps adding the same entry to the table even though I have set IGNORE INTO.

3
  • If the IP is the only field on the table, just make it primary key and there will be no duplicates Commented May 13, 2012 at 22:49
  • Thanks that worked just fine, I didn't know you could do that :) Least I'm learning, however for future reference if this wasn't the only field on the table how would I go about doing this? Thanks Commented May 13, 2012 at 22:55
  • I'll edit this as an answer so you can accept it, If you have more field you just need to identify the fields that need to keep unique and make all Primary key Commented May 13, 2012 at 22:56

3 Answers 3

1

It looks like you don't have a UNIQUE INDEX on the IP column. In order for INSERT IGNORE to work as required, that's neccessary. Try this;

ALTER TABLE ip ADD UNIQUE(ip)

To remove duplicates already, you can run this.

ALTER IGNORE TABLE ip ADD UNIQUE(ip)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I set the row to the primary key to fix the problem , but yes I don't have a Unique Index, at least now I know how to fix this in the future if I have more than one field.
FYI, if your table is InnoDB, this can be a bad idea. InnoDB treats the primary key slightly differently than MyISAM. Typically best practice is to create an autoincrementing Primary Key, and then put a UNIQUE index on the data you need to be unique. Hope that helps.
0

Why shouldn't it? ignore just ignores errors. Make the ip unique.

alter table storip add unique (ip);

Comments

0

If the IP is the only field on the table, just make it primary key and there will be no duplicates

alter table storeip add primary key (ip);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.