Senthilkumar Posted May 7 Share Posted May 7 Dear Team, I have PHP code which is used to upload the data from CSV files to the database. My code is <?php // Load the database configuration file define("HOST", 'localhost'); define("USERNAME", 'root'); define("PASSWORD", '123456'); define("DATABASE", 'sbms'); // default database name function pdoConnect($dbname = DATABASE) { $db = new PDO("mysql:host=" . HOST . ";dbname=$dbname;charset=utf8", USERNAME, PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } $pdo = pdoConnect(); if (isset($_POST['importsubmit'])) { $file = $_FILES['file']['tmp_name']; $fp = fopen($file, 'r'); $req_cols = [ 0 => 'sales_doc_type', 2 => 'billing_date', 11 => 'material', 12 => 'description', 14 => 'billed_quantity', 25 => 'gross_amount', 38 => 'sold_party', 39 => 'customername', 45 => 'sales_office', 46 => 'plant', 63 => 'dchannel', 64 => 'distribution_channel_description', 73 => 'division', 74 => 'division_header' ]; $import_data = []; fgetcsv($fp); // discard the header row while ($allrow = fgetcsv($fp)) { $row = array_intersect_key($allrow, $req_cols); if (empty($row[0])) continue; // skip rows where no value in first col $randdays = rand(5, 30); $row[2] = date('Y-m-d', strtotime($row[2])); $row[25] = str_replace(',', '', $row[25]); $row[39] = str_replace("'", "", $row[39]); $row[12] = str_replace("'", "", $row[12]); $import_data[] = vsprintf("('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", $row); } fclose($fp); $chunks = array_chunk($import_data, 3000); foreach ($chunks as $ch) { $sql = "INSERT IGNORE INTO billing (sales_doc_type, billing_date, material, description, billed_quantity,gross_amount, sold_party, customername, sales_office, plant, dchannel, distribution_channel_description, division, division_header) VALUES " . join(',', $ch); $pdo->exec($sql); } } // Redirect to the listing page header("Location: Billing.php"); ?> I am using two CSV files (April Service.csv and APRIL SPARE.csv). When I am importing April Service.csv, it is updating properly. But when I am importing APRIL SPARE.csv file I get the following error Fatal error: Uncaught ValueError: The arguments array must contain 14 items, 12 given in C:\Users\senthilkumar.rp\OneDrive - SCHWING Stetter (India) Private Limited\SBMS\sbms\sbms\SuperAdmin\Sales_Import.php:55 Stack trace: #0 C:\Users\senthilkumar.rp\OneDrive - SCHWING Stetter (India) Private Limited\SBMS\sbms\sbms\SuperAdmin\Sales_Import.php(55): vsprintf() #1 {main} thrown in C:\Users\senthilkumar.rp\OneDrive - SCHWING Stetter (India) Private Limited\SBMS\sbms\sbms\SuperAdmin\Sales_Import.php on line 55 I uploaded the CSV files and table here for your reference on the below link. https://drive.google.com/drive/folders/1UIVW6Ya5D2shwwvxEK6WiyBYSVCdNTtX?usp=sharing I think there is some mistake on the csv file. I am not able to identify that problem. Can any one help me solve this problem. Quote Link to comment https://forums.phpfreaks.com/topic/320483-uncaught-valueerror-while-uploading-csv-file/ Share on other sites More sharing options...
Barand Posted May 7 Share Posted May 7 I think you will find that line no 21374 in the "spare" csv has only 73 values instead of the expected 75. Quote Link to comment https://forums.phpfreaks.com/topic/320483-uncaught-valueerror-while-uploading-csv-file/#findComment-1623496 Share on other sites More sharing options...
Senthilkumar Posted May 7 Author Share Posted May 7 Dear Mr.Barand, Thanks for your suggestion. I found the mistake in line number 21376 column M, the value entered with \ (Bend DN125 5 1/2" 32,5° S2000\). Because of that the data is not imported. I removed that special character and imported the data. How to avoid such type of mistakes in features Quote Link to comment https://forums.phpfreaks.com/topic/320483-uncaught-valueerror-while-uploading-csv-file/#findComment-1623499 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.