0

I have serious question about importing data from CSV to Database. Import script:

if (file_exists('temp.csv')) {   

        $i=0;
        require "connection.php";
        $handle = fopen("temp.csv", "r");
        try {
  $import= $db->prepare("INSERT INTO adherence(
                          dateandtime,
                          lastname,
                          firstname,
                          paidtime,
                          approvedtime,
                          notadhering) VALUES(
                          ?,?,?,?,?,?)");
$i = 0;        
while (($data = fgetcsv($handle, 1000, ",", "'")) !== FALSE) {
    if($i > 0) {
        $data = str_replace('"', '', $data); 
        $myDate =  date("Y/m/d",strtotime(str_replace('/','-',$data[0])));
        $import->bindParam(1, $myDate, PDO::PARAM_STR);             
        $import->bindParam(2, $data[1], PDO::PARAM_STR);                
        $import->bindParam(3, $data[2], PDO::PARAM_STR);                
        $import->bindParam(4, $data[3], PDO::PARAM_STR);                
        $import->bindParam(5, $data[4], PDO::PARAM_STR);
        $import->bindParam(6, $data[5], PDO::PARAM_STR);            
        $import->execute();
    }
    $i++;
}
fclose($handle);

Problem is, I need some sort of conditional logic to check, if row allready exist in database before importing, and skip it - if it exist. How to handle this kind of thing?

3
  • SO - what's the criteria that row exists? Commented Dec 2, 2015 at 19:47
  • 1
    Add those attributes/columns that make 2 rows a duplicate as a mysql key/index. Then use "insert ignore..." to insert only non existing rows. Commented Dec 2, 2015 at 19:48
  • Problem is, that there is some tables, that have 8 items in table, 7 of them are like keys. Commented Dec 2, 2015 at 19:49

1 Answer 1

2

Basically you have two different ways to approach it.

1. via the rdbms:

Use an unique index in your table. Once you insert a duplicate, you'll encounter an error, which can be properly displayed/logged/whatever.

2. via application logic:

Search for your item before inserting with a proper SELECT statement.. If you find a match, don't insert it.

Example:

$sth = $db->prepare("SELECT yourfields FROM yourtable WHERE yourcondition = :cond");
$sth->bindParam(':cond',$yourvariable, PDO::PARAM_STR);
$sth->execute();

if ($sth->rowCount() > 0) {
    // results - don't insert
} else {
    // place your insert terms here
}

In most conditions, coders will implement the first way, since it reduces traffic between application and RDBMS and makes your data model more robust. If this is any issue, try the second way.

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

3 Comments

Thank you. By any chance, maybe you have any example of 2nd way?
Option 2 isn't really an option
@JustinasT I've added an example.

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.