我希望能够拥有一个包含6列的CSV文件,我正在努力找出最佳方法。
这是我现在拥有的,它适用于2列,
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h2>Updated:</h2>";
readfile($_FILES['filename']['tmp_name']);
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$shipdate = date('Y-m-d');
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$import="UPDATE table
SET abc='$data[1]', def='".$shipdate."'
WHERE g=$data[0]";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
print "</br>Updated Successfully</br>";
//view upload form
}else { ?>
Import Numbers<br />
<form enctype='multipart/form-data' action='upload.php' method='post'>
<input size='50' type='file' name='filename'><br />
<input type='submit' name='submit' value='Upload'></form>
<?php
}
?>
我想现在只有当列中有值时才更新第3-6列,我尝试过:
但它没有用,我假设是因为我不能在while子句之外引用$ data [x]?
非常感谢任何帮助。
REPLACE INTO
会在这里工作吗?