我尝试上传csv并将其写入SQL数据库。
以下是我正在使用的代码。
upload.php的
<body>
<div id="container">
<div id="form">
<?php
include "connection.php"; //Connect to Database
$deleterecords = "TRUNCATE TABLE price_upload"; //empty the table of its current records
mysql_query($deleterecords);
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
echo "<h2>Displaying contents:</h2>";
//readfile($_FILES['filename']['tmp_name']);
}
//exclude the first row
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$firstRow = true;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($firstRow) { $firstRow = false; }
else {
$import = "INSERT into price_upload(Buy_Grp, Item_No, Item_Desc, Manufacturer, Department, Barcode, Range ID, Planogram, Start_Price, Trading_Term, Basket, End_Date, Min_Qty, Assorted_Qty, Discount) values ('".$data[0]."',".$data[1].",'".$data[2]."','".$data[3]."','".$data[4]."',".$data[5].",'".$data[6]."','".$data[7]."','".$data[8]."','".$data[9]."','".$data[10]."','".$data[11]."',".$data[12].",'".$data[13]."','".$data[14]."')";
//print $import;
mysql_query($import) or die(mysql_error());
}
}
fclose($handle);
print "Import done";
//view upload form
} else {
print "Upload new csv by browsing to file and clicking on Upload<br />\n";
print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
print "File name to import:<br />\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Upload'> </form>";
}
?>
</div>
</div>
</body>
当我运行它时,没有数据写入数据库。
当我在屏幕上打印$ import时,我明白了。
INSERT into price_upload(Buy_Grp, Item_No, Item_Desc, Manufacturer,
Department, Barcode, Range ID, Planogram, Start_Price, Trading_Term, Basket,
End_Date, Min_Qty, Assorted_Qty, Discount) values ('11',31/01/17,'9','-
','8.00% Buying',53629,'MORTEIN COIL BURNER 30 CT6','RECKITT BENCKISER','-
','19300700000000','-','-',26.18,'0.00%','107411')INSERT into
price_upload(Buy_Grp, Item_No, Item_Desc, Manufacturer, Department, Barcode,
Range ID, Planogram, Start_Price, Trading_Term, Basket, End_Date, Min_Qty,
Assorted_Qty, Discount) values ('ams',7.35,'1.00%','-','-',1,'-','0.00%
Buying','53835','COLGATE T/B TEENS DESIGN IT','COLGATE PALMOLIVE PTY
LTD','Oral Care',9300630000000,'-','url').....
数据似乎没有提取正确的值或列。
不确定我错过了什么或做错了什么。
任何人都可以提供帮助吗?