gpt4 book ai didi

php - 使用php将excel数据导入数据库表

转载 作者:行者123 更新时间:2023-11-29 16:06:59 26 4
gpt4 key购买 nike

问题出在我的 Excel 中,有 369 行。当我回显/打印该数据时,它显示正确,但是当我在数据库表中插入相同的数据时,仅插入了 18 - 30 条记录。

if (isset($_POST['Submit'])) {
$file = $_FILES['csv_file']['tmp_name'];
$handle = fopen($file, "r");
if ($file == NULL) {
error(_('Please select a file to import'));
redirect(page_link_to('excel_data_upload'));
}else {
$conn = connect();
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$num3 = $filesop[3];
$num8 = $filesop[8];
$num9 = $filesop[9];
$num20 = $filesop[20];
if($num3!='ExpiryDate' &&$num8!='VCNO' &&$num20!='TotalB2CAmount' && $num9 !='STBNO'){

$insertAgent = mysqli_query($conn, "INSERT INTO `upload_billing_data`
(`vc_number`,`stb_number`,`operator_id`,`expiry_date`,`monthly_bill_amount`)
VALUES ('$num8','$num9',140,'$num3','$num20')");

if($insertAgent)
{
echo 'succss';
}else{
echo 'error';
}

}
}
close($conn);

}
}

我正在从 Excel 数据中获取数据。我想插入所有记录

最佳答案

更改如下代码,您可能会使用一次查询将所有数据保存到数据库:

$query_insert = array();

while(($filesop = fgetcsv($handle, 1000, ",")) !== false) {
$num3 = filterString($filesop[3]);
$num8 = filterString($filesop[8]);
$num9 = filterString($filesop[9]);
$num20 = filterString($filesop[20]);

if ($num3!='ExpiryDate' &&$num8!='VCNO' &&$num20!='TotalB2CAmount' && $num9 !='STBNO') {
$query_insert[] = "('{$num8}', '{$num9}', 140, '{$num3}', '{$num20}')";
}
}

// If no row matched your if, then there will be no row to add to the database
if (count($query_insert)>0) {
$conn = connect();

$query_insert_string = implode(', ', $query_insert);

$query = "INSERT INTO `upload_billing_data` (`vc_number`, `stb_number`, `operator_id`, `expiry_date`, `monthly_bill_amount`) VALUES {$query_insert_string};";
$insertAgent = mysqli_query($query);

// The rest of you code
...

close($conn);
}



// This function makes sure that you string doesn't contain characters that might damage the query
function filterString($string) {
$string = str_replace(array("\'", '"'), array('', ''), $string);
$string = filter_var($string, FILTER_SANITIZE_STRING);
return $string;
}

关于php - 使用php将excel数据导入数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com