我正在尝试将 csv 文件导入 mysql-databse,并且我希望上传脚本检查重复项,如果发现任何重复项,它应该更新该行,但只更新选定的值。我希望它更新除“首次看到”之外的所有值。bssid 是我的唯一标识符。我也有建议我应该将“addslashes”更改为一些更好的代码,但我不知道哪种方式最好,有什么建议吗?
<?php
//Count ap´s before import
$before = mysql_query("SELECT * FROM wifi");
$num_rows_before = mysql_num_rows($before);
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$linesCount ++;
mysql_query("INSERT INTO wifi (bssid, channel, privacy, ciper, auth, power, essid, latitude, longitude, first_seen, last_seen) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."',
'".addslashes($data[10])."'
)
");
}
} while ($data = fgetcsv($handle,1000,","));
//redirect
header('Location: index.php/ladda-upp?success=1&before=' . $num_rows_before); die;
}
//Catch argument from url
$arg_bef=$_GET['before'];
//Count ap´s after import
$after = mysql_query("SELECT * FROM wifi");
$num_rows_after = mysql_num_rows($after);
//Count new rows
$new_rows = $num_rows_after - $arg_bef;
//generic success notice
if (!empty($_GET[success])) { echo "<br><b>Resultat: Din fil har blivit importerad!</b><br><br>";
//echo stats
echo "Antal före import - ";
echo "$arg_bef";
echo "<br>";
echo "Antal efter import - ";
echo "$num_rows_after";
echo "<br>";
echo "Antal nya rader - ";
echo "$new_rows";
echo "<br>";
echo "Rader i CSV-fil - ";
}
//Close connection to databse
mysql_close($connect) ;
?>