I have this php code that is aimed for importing all rows and columns in csv file into mysql table as records. It successfuly inserts from csv into mysql table called "import_items" BUT if i re-do the import, it will insert duplicate records. All i need is where to fit the Query Update in which it should check IF ITEM EXISTS in table name-> Update values , IF NOT EXISTS..Insert record!
Here is my code :
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
File to import:<br />
<input size='30' type='file' name='filename'>
<input type="submit" name="submit" value="Upload"></form>
<?php
require_once('connect_db.php');
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
//Import uploaded file to Database
$row = 1;
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//Update Database Values
$import="insert into import_items (item_no,qty,actual_price,discount_price,difference_price,date) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."', '".mysql_real_escape_string($data[2])."', '".mysql_real_escape_string($data[3])."', '".mysql_real_escape_string($data[4])."', '".$date = date('Y-m-d')."')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
}
}
?>
Thank You