dtch60248 2018-10-01 02:54
浏览 271

PHP:导入包含空单元格/列的CSV文件时,列数不匹配

I have a code that will allow users to import/insert data into database from the CSV file. the problem is that if there is an empty columns or cells from the CSV file, it will display an error telling me that the column count doesn't match value count at row 1. I tried to assign a value in the empty cells/columns of the CSV file and it successfully uploaded. How can I allow the users to upload a CSV file even if there's an empty columns/cells? Below is my code.

            if ($handle) {
                $counter = 0;
                $sql ="INSERT IGNORE INTO bill_of_materials(allotment_code, category_name, activity, quantity, unit, description, end_unit_quantity, unit_cost, regular_labor_cost, end_unit_labor_cost, type, batch) VALUES";

                while (($line = fgets($handle)) !== false) {

                    $sql .= "('".implode("', '", explode("|", addslashes(str_replace('"', '', $line))))."'),";
                    $counter++;
                }

                $sql = substr($sql, 0, strlen($sql) - 1);

                 if (mysqli_query($new_conn, $sql) === TRUE) {

                    $message = 1;

                } else {

                    echo mysqli_error($new_conn);
                }
                fclose($handle);

here is my table: my table

and here's the csv file: csv file

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据