How to ignore a duplicate field and proceed with insertion?

Asked

Viewed 767 times

3

I have a file txt that I am extracting information and in it the information is repeated 6 times, which inserts me in the database the same record also 6 times.

Setei UNIQUE for a unique number to each record in the mysql database but when trying to run sql that inserts the data, gives a Entrada '120000001016' duplicada para a chave 'SQ_CANDIDATO' and the insertion process is completed.

How could I solve this problem by entering only once each record in the bank without having to treat the txt?

foreach ($itens as $item) {

$dados      = explode(';', $item);

mysql_query("INSERT INTO VOT_CAND_MUN_ZONA VALUES ( 

                        '',
                        '".utf8_decode(trim($dados[0], '"'))."',
                        '".utf8_decode(trim($dados[1], '"'))."',
                        '".utf8_decode(trim($dados[2], '"'))."',
                        '".utf8_decode(trim($dados[3], '"'))."',
                        '".utf8_decode(trim($dados[4], '"'))."',
                        '".utf8_decode(trim($dados[5], '"'))."',
                        '".utf8_decode(trim($dados[6], '"'))."',
                        '".utf8_decode(trim($dados[7], '"'))."',
                        '".utf8_decode(trim($dados[8], '"'))."',
                        '".utf8_decode(trim($dados[9], '"'))."',
                        '".utf8_decode(trim($dados[10], '"'))."',
                        '".utf8_decode(trim($dados[11], '"'))."',
                        '".utf8_decode(trim($dados[12], '"'))."', <---- UNIQUE
                        '".utf8_decode(trim($dados[13], '"'))."',
                        '".utf8_decode(trim($dados[14], '"'))."',
                        '".utf8_decode(trim($dados[15], '"'))."',
                        '".utf8_decode(trim($dados[16], '"'))."',
                        '".utf8_decode(trim($dados[17], '"'))."',
                        '".utf8_decode(trim($dados[18], '"'))."',
                        '".utf8_decode(trim($dados[19], '"'))."',
                        '".utf8_decode(trim($dados[20], '"'))."',
                        '".utf8_decode(trim($dados[21], '"'))."',
                        '".utf8_decode(trim($dados[22], '"'))."',
                        '".utf8_decode(trim($dados[23], '"'))."',
                        '".utf8_decode(trim($dados[24], '"'))."',
                        '".utf8_decode(trim($dados[25], '"'))."',
                        '".utf8_decode(trim($dados[26], '"'))."',
                        '".utf8_decode(trim($dados[27], '"'))."',
                        '".utf8_decode(trim($dados[28], '"'))."'

)") or die (mysql_error());
  • 1

    It is an Insert with several values then?

  • Is inside a foreach

  • 1

    Remove the die so it passes to the next line of the array and makes the next Insert.

  • Perfect @rray put Unique but forgot to remove the die.

2 answers

3

Use the keyword IGNORE to ignore any errors (including duplicated lines) in your query:

INSERT IGNORE INTO ...

Of mysql documentation:

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are Ignored. For example, without IGNORE, a Row that Duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a Duplicate-key error and the statement is aborted. With IGNORE, the Row is discarded and no error occurs. Ignored errors may generate Instead, Although Duplicate-key errors do not.

IGNORE has a similar Effect on Inserts into partitioned Tables Where no Partition matching a Given value is found. Without IGNORE, such INSERT statements are aborted with an error; However, when INSERT IGNORE is used, the Insert Operation fails Silently for the Row containing the Unmatched value, but any Rows that are Matched are inserted. For an example, see Section 19.2.2, "LIST Partitioning".

Data conversions that would Trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the Closest values and inserted; warnings are produced but the statement does not abort. You can determine with the mysql_info() C API Function how Many Rows Were Actually inserted into the table.

2


The insertion process is for why you are called die() if the mysql_query fail, it makes the script end in a way similar to a Exit. Removing the die() the error will still happen but the next item of the array will be evaluated in the query.

The ideal is to record the errors of mysql_error() in a log the part.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.