CSV upload
2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 20, 2010 (RSS)
We are looking to populate a listing page by processing a CSV which has been uploaded to the server.
We have it populating the first time through fine, but now we need to add update capabilities to the script so that the duplicate data is not added a second time.
Thank you!
Chris
We have it populating the first time through fine, but now we need to add update capabilities to the script so that the duplicate data is not added a second time.
<?php
include "connect.php";
if(isset($_POST['submit'])) {
$filename=$_POST['filename'];
$row = 1;
$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ( $row == 1){
$row++;
} else {
$import="REPLACE INTO `cms_ourcustom_list` SET
title = '".mysql_real_escape_string($data[0])."',
lns = '".mysql_real_escape_string($data[1])."',
proprietor = '".mysql_real_escape_string($data[2])."',
address = '".mysql_real_escape_string($data[3])."',
city = '".mysql_real_escape_string($data[4])."',
zip = '".mysql_real_escape_string($data[5])."',
phone = '".mysql_real_escape_string($data[6])."',
website = '".mysql_real_escape_string($data[7])."'";
mysql_query($import) or die(mysql_error());
}
}
fclose($handle);
print "Import done";
} else {
print "<form action='csv_up.php' method='post'>";
print "Type file name to import:<br>";
print "<input type='text' name='filename' size='20'><br>";
print "<input type='submit' name='submit' value='submit'></form>";
}
?>
Thank you!
Chris
Re: [Christopherb] CSV upload
By Jason - September 20, 2010
Hi Chris,
What you will need is a field (or fields) from the CSV file that you can use to uniquely define a record.
So, for example, if a combination of title and website could uniquely describe a record, we can use these to check if the record already exists:
In this example, it searches for the number of records in ourcustom_list that have the title and the website of the current CSV record. If it returns 0, it means the record is not yet in the database. You can then add code to add it in. Note: you many need to change names to match what is in your database.
Hope this helps.
What you will need is a field (or fields) from the CSV file that you can use to uniquely define a record.
So, for example, if a combination of title and website could uniquely describe a record, we can use these to check if the record already exists:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ( $row == 1){
$row++;
} else {
$where = "title ='".mysql_escape($data[0])."' AND website='".mysql_escape($data[7])."'";
$recCount = mysql_select_count_from('ourcustom_list',$where);
if($recCount==0){
//INSERT RECORD INTO THE DATABASE//
}
}
}
In this example, it searches for the number of records in ourcustom_list that have the title and the website of the current CSV record. If it returns 0, it means the record is not yet in the database. You can then add code to add it in. Note: you many need to change names to match what is in your database.
Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/