我在数据库中上传csv文件已经完成但是当我上传csv文件并显示数据表时,它会在表中上传一行。 就像我上传10个数据但在数据表中插入11行。
// path where your CSV file is located
define('csvfile/',
'plivo/dialer/csvfile/');
// Name of your CSV file
$csv_file = $target_dir . "$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];
}
echo '<br>'. $col1 = $col[0];
echo 'data'.$col1.'<br>';
if($col1 != ' ' || $col1 != NULL){
// SQL Query to insert data into DataBase
$query = "INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('$caller_id','" . $col1 . "','$play_url','$row_id')";
print_r("INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('$caller_id','" . $col1 . "','$play_url','$row_id')");
$s = mysql_query($query);
if ($s) {
echo 'pass';
header("location:dashboard.php");
} else {
echo 'fail<br>';
}
}
}
fclose($handle);
}
当我打印查询显示
时 1data1
INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('9988776655','1','www.test.com','15')
2data2
INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('9988776655','2','www.test.com','15')
3data3
INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('9988776655','3','www.test.com','15')
4data4
INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('9988776655','4','www.test.com','15')
5data5
INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('9988776655','5','www.test.com','15')
data
INSERT INTO callblast(`callerid`, `phoneno`, `playurl`,`campaignid`) VALUES('9988776655','','www.test.com','15')
最后的数据就像这样添加了
在数据库中添加更多行的问题是什么。
请帮忙。
谢谢。
答案 0 :(得分:1)
您应以$col1 != ' '
方式检查:trim($col1) != ''
,因为文件的最后一行包含EOF / EOL和更多空格/标签。