Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:38:21+00:00 2026-05-28T03:38:21+00:00

I am using the following script to upload records to my MYSQL database, the

  • 0

I am using the following script to upload records to my MYSQL database, the problem I can see is if a client record is uploaded and it already exists in the database and is duplicated.

I have seen lots of posts on here about people asking on how to remove duplicates from the csv file itself on upload, e.g if there are two instances of the name bob and the postcode lh456gl in the csv dont upload it, but what I want to know is if its possible to check the database for a record first before adding that record so not to insert a record that already is there.

So something like :

if exist namecolumn=$name_being_inserted and postcode=postcode_being_inserted then
  do not add that record.

Is this even possible to do ?.

<?php
//database connect info here

//check for file upload
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){

    //upload directory
    $upload_dir = "./csv";

    //create file name
    $file_path = $upload_dir . $_FILES['csv_file']['name'];

    //move uploaded file to upload dir
    if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {

        //error moving upload file
        echo "Error moving file upload";

    }

    //open the csv file for reading
    $handle = fopen($file_path, 'r');

    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {

        //Access field data in $data array ex.
        $name = $data[0];
        $postcode = $data[1];

        //Use data to insert into db
        $sql = sprintf("INSERT INTO test (name, postcode) VALUES ('%s','%s')",
                    mysql_real_escape_string($name),
                    mysql_real_escape_string($postcode)
                    );
        mysql_query($sql) or (mysql_query("ROLLBACK") and die(mysql_error() . " - $sql"));
    }

    //delete csv file
    unlink($file_path);
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:38:21+00:00Added an answer on May 28, 2026 at 3:38 am

    There are two pure MySQL methods that I can think of that would deal with this issue. REPLACE INTO and INSERT IGNORE.

    REPLACE INTO will overwrite the existing row whereas INSERT IGNORE will ignore errors triggered by duplicate keys being entered in the database.

    This is described in the manual as:

    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.

    For INSERT IGNORE to work you will need to setup a UNIQUE key/index on one or more of the fields. Looking at your code sample though you do not have anything that could be considered unique in your insert query. What if there are two John Smiths in Wolverhampton? Ideally you would have something like an email address to define as unique.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following script to import data into my mysql database from
All, I'm using the following jQuery plugin: https://github.com/blueimp/jQuery-File-Upload You can see that in the
I'm using the following jQuery ( https://github.com/blueimp/jQuery-File-Upload ) script to upload files. This script
I'm using the following script in my website in order to create pagination next-previous
I'm using the following script to request new content to a file called index.html:
I'm using the following script to force a specific page - when loaded for
I have Divs show and hide (with an animation) using the following script (I
Ok, I have been using the following script for over a year now to
Using the following Webservice definition using aClientArgs as a complex type: [System.Web.Script.Services.ScriptService] public class
I am using the following code as part of an autocomplete script to avoid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.