I want to add data to database via csv file. Am able to achieve this but whenever i upload that file again it gets duplicated, I dont want the data of a particular row to get entered again in a row but a specific column field may get repeat.The following code is for upload file am having database with table location having fields for ID(auto incremented), State, City.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require ('dbconfig.php');
if(isset($_POST['submit']))
{
$fname = $_FILES['sel_file']['name'];
echo 'upload file name: '.$fname.' ';
$chk_ext = explode(".",$fname);
if(strtolower(end($chk_ext)) == "csv")
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename,"r");
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
{
$sql = "INSERT into location(State,City,District) values('$data[0]','$data[1]','$data[2]')";
mysqli_query($conn,$sql) or die ;
}
fclose($handle);
echo "Successfully imported";
}
else
{
echo "Invalid File";
}
}
?>
<h1>import CSV file</h1>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post' enctype="multipart/form-data">
Import File: <input type='file' name='sel_file' size='20'>
<input type='submit' name='submit' value='submit'>
</form>