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.

<?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