这可能看起来像是重复但我已经阅读了Stack Over Flow上的所有内容,其中一些内容太旧而无法工作以及其他我无法工作或不理解的内容。
我需要带一个.csv文件并将其上传到我的php脚本中并将数据放入数据库中。除了某些字段可能在数据中包含逗号外,我的一切都运行良好。我的计划将其视为一个新领域。我需要将数据保存在该字段中。
这就是我现在拥有的......
$cols = 0;
$count = '5000';
if ($_FILES[csv][size] > 0) {
$dup = 0;
$skipfirst = 0;
$filename = $_FILES[csv][name];
$file = $_FILES[csv][tmp_name];
$noOfLines = count(file($file));
$ext = substr($filename, -3);
if ($ext != "csv") {
header('Location: https://example.com?msg=notcsv');
exit;
}
if ($noOfLines > $count) {
header('Location: https://example.com?msg=toolarge');
exit;
}
$handle = fopen($file, "r");
// loop through the csv file and insert into database
do {
if ($data[0]) {
$col3 = $data['1']; // username - handle
$col4 = $data['4']; // name
$col5 = $data['22']; // saddress1
$col6 = $data['24']; // saddress2
$col7 = $data['28']; // scity
$col8 = $data['30']; // sstate
$col9 = $data['33']; // szip
$col10 = $data['35']; // scountry
$col1 = $data['37']; // order number
$col2 = $data['38']; // order date
$col11 = $data['52']; // item1
$col12 = $data['55']; // qty1
$col13 = $data['63']; // item2
$col14 = $data['66']; // qty2
$col15 = $data['74']; // item3
$col16 = $data['77']; // qty3
$col17 = $data['85']; // item4
$col18 = $data['88']; // qty4
$col19 = $data['96']; // item5
$col20 = $data['99']; // qty5
$col21 = $data['107']; // item6
$col22 = $data['110']; // qty6
$col23 = $data['118']; // item7
$col24 = $data['121']; // qty7
$col25 = $data['129']; // item8
$col26 = $data['132']; // qty8
$col27 = $data['140']; // item9
$col28 = $data['143']; // qty9
$col29 = $data['151']; // item10
$col30 = $data['154']; // qty10
if ($skipfirst != '0') { // made to skip first row because of headers
$result = mysqli_query($connect, "SELECT id FROM information WHERE order_number='$col1' LIMIT 1");
$myrow = mysqli_fetch_array($result);
if ($myrow[id] == '') {
mysqli_query($connect, "INSERT INTO information (order_number,orderdate,username,name,saddress1,saddress2,scity,sstate,szip,scountry,item1,qty1,item2,qty2,item3,qty3,item4,qty4,item5,qty5,item6,qty6,item7,qty7,item8,qty8,item9,qty9,item10,qty10,complete) VALUES ('$col1','$col2','$col3','$col4','$col5','$col6','$col7','$col8','$col9','$col10','$col11','$col12','$col13','$col14','$col15','$col16','$col17','$col18','$col19','$col20','$col21','$col22','$col23','$col24','$col25','$col26','$col27','$col28','$col29','$col30','0')");
}
else {
$dup++;
}
} // end if skipfirst = 0
$skipfirst++;
}
}
while ($data = fgetcsv($handle, 5000, ",", "'"));
//
mysqli_close($connect);
$count = $count - $noOfLines;
$msg = "success";
}
示例数据可能是......
"12234","56 My St, My Town","NC","434455","Basket Item,Well"
我把引号用来表示每个字段。当然,逗号可以随时在任何或所有字段中。希望有道理。
感谢您的帮助。
答案 0 :(得分:0)
在您加载数据时,您将机箱字符作为单引号,您应该使用双引号:
while ($data = fgetcsv($handle, 5000, ",", "\""));