add data from csv file to mysql database avoiding duplicates in php -


i want add data database via csv file. able achieve whenever upload file again gets duplicated, dont want data of particular row entered again in row specific column field may repeat.the following code upload file having database table location having fields 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 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> 

the first way: combine unique state,city,district :

alter table `db name`.`table name` add unique (`state`, `city`, `district`); 

and insert query should in try catch

the second way: after inserting should manually delete duplicates table how delete duplicates on mysql table?


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -