While working on a project, hosted locally, I'm stuck at managing CSV uploads. One of tasks require me to upload data on daily basis that has either new entries or updated status for existing entries. There is also an probability that some of the entries (that exists in database) has no updated status.
Problem statement;
I've created a CSV upload feature that uploads the CSV file to a particular location and imports the information in assigned TABLE. I want to know on what is the best way to verify the database records when I do the CSV upload.
It should ideally work as following;
- if entry doesn't exists (INSERT new entry basis data from CSV file)
- if the entry exists and has status SAME as the new uploaded CSV file (IGNORE & do nothing)
- if the entry exists and has DIFFERENT status than the one in new uploaded CSV file (UPDATE status to what is mentioned in CSV file)
Database / CSV file structure
- tracking_id (auto increment)
- odanumber (uploaded through CSV & can have duplicate entries)
- airwaybill (uploaded through CSV & UNIQUE)
- courierful (uploaded through CSV & can have duplicate entries)
- delstatus (uploaded through CSV & is what gets updated mostly)
- deliverydate (uploaded through CSV & gets updated with each delivery)
From the above, delstatus gets updated almost each time (for existing entries) the new CSV is uploaded and hence needs to be checked.
I assume that we can pick 'airwaybill' to check if it exists, and if it does, check if the delstatus is same as of CSV file or update. If 'airwaybill' doesn't exist then a new records must be added to the database. As that would save me from entering all records in database unnecessarily. Or can be done may be in a better way (that I'm yet to explore).
What's happening right now;
I'm able to upload the complete set of CSV file, creating new entries in database through following code.
<?php
if(isset($_POST['csv']))
{
$sqlname= 'localhost';
$username= 'root';
$table= 'tracking';
$password= '';
$db='aatrack';
$file=$_POST['csv'];
$cons= mysqli_connect("$sqlname", "$username","$password","$db") or die(mysql_error());
$result1=mysqli_query($cons,"select count(*) count from $table");
$r1=mysqli_fetch_array($result1);
$count1=(int)$r1['count'];
mysqli_query($cons, '
LOAD DATA LOCAL INFILE "'.$file.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
')or die(mysql_error());
$result2=mysqli_query($cons,"select count(*) count from $table");
$r2=mysqli_fetch_array($result2);
$count2=(int)$r2['count'];
$count=$count2-$count1;
if($count>0)
{
header("location:success.php?id=$count");
}
}
?>
Can you please help in guiding the best way possible to achieve the same. I understand that it can be done by first uploading the information to a temp_table and comparing the same before entries are updated in the LIVE table.
Please suggest an optimum way to achieve the results.
Thank you for reading this far.
Best regards,
Amit Agnihotri
There are two scenarios here:
the table's columns exactly match the csv columns. in that case
REPLACEis the answer - it's a keyword to theLOAD DATA INFILEsee doc entrythe table's columns don't match the csv columns: REPLACE would cause conflicting records to be removed and reinserted, effectively removing the additional data. In which case
LOAD DATA INFILEis not effective by itself, you need another approach with either filtering your file before, doing updates via php or some other method.In any case, if you want to add more "logic" to the import process, maybe
LOAD DATA INFIlEisn't really the right approach, but using temp tables may very well be to benefit from all the goodness databases provide.