Hi i am importing a csv through a script the table structure is as below
`contact_id` int(11) NOT NULL auto_increment,
`contact_first` varchar(255) character set latin1 default NULL,
`contact_last` varchar(255) character set latin1 default NULL,
`contact_email` varchar(255) character set latin1 default NULL,
PRIMARY KEY (`contact_id`)
and the data in csv is like this
Jim,Smith,jim@tester.com
Joe,Tester,joe@tester.com
and the query i am using for insert is as below
mysql_query("INSERT IGNORE INTO contacts (contact_first, contact_last, contact_email) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
i have made use of ignore function in the query but it doesnot work and it keeep on onserting the same value
The IGNORE
keyword will only have your desired impact on duplicate rows.
A row will only be recognized as a duplicate if you have a primary key or unique key on the appropriate columns.
You haven't really explained exactly what the issue is, but I suspect you might want to create a unique key on the contact_email
column to prevent duplicates from being inserted.
I would also suggest at a minimum using mysql_escape_string
instead of addslashes
to ensure the strings are encoded correctly.
There will likely be comments on the fact that the mysql_* functions are deprecated so you should look into PDO or mysqli_* functions.
If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead Refer DOCS
The use of IGNORE is :
If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. 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 still is not inserted, but no error is issued.
in your case you are inserting three values which are same but as the ID is auto incremental
which will insert new record with same values which is obvious. If you want to prevent it from inserting in to the database you have to add unique index to one of the other column
hope it will help!