我想通過csv文件添加數據到數據庫。我能夠實現這一點,但每當我再次上傳該文件它得到重複,我不希望特定行的數據再次進入一行,但一個特定的列字段可能會重複。下面的代碼是上傳文件我有具有表格位置的數據庫具有用於ID(自動遞增),狀態,城市的字段。從csv文件添加數據到mysql數據庫,避免重複在php
<?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>
得到解決方案使用插入忽略成(........),同時使城市獨特的表中 –