嗨我目前有這個PHP文件,每次觸發時都會將csv數據導入到mysql數據庫中,但是我希望它覆蓋數據庫中的當前數據,而不是每次觸發時添加它。這裏是文件減我的細節:用PHP上傳到MYSQL的CSV
<?php
//database connection details
$connect = mysql_connect('mydetails','mydetails','mydetails');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
//your database name
$cid =mysql_select_db('mydetails',$connect);
// path where your CSV file is located
define('CSV_PATH','myfile location');
// Name of your CSV file
$csv_file = CSV_PATH . "my file";
if (($handle = fopen($csv_file, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
$col1 = $col[0];
$col2 = $col[1];
$col3 = $col[2];
$col4 = $col[3];
// SQL Query to insert data into DataBase
$query = "INSERT INTO orders(OrderNumber,OrderCustomer,OrderReqdQty,OrderStatus) VALUES('".$col1."','".$col2."','".$col3."','".$col4."')";
$s = mysql_query($query, $connect);
}
fclose($handle);
}
echo "File data successfully imported to database!!";
mysql_close($connect);
?>
TODO: 1.檢查'OrderNumber'如果它存在於你的數據庫 2.如果存在的話做一個UPDATE語句 3.如果不存在這樣做和INSERT語句 –
<插入強制**不使用mysql驅動程序,它的死**消息在這裏> – DevDonkey