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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!