最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is that only the first row getting inserted into Mysql table when i import csv file on backend custom plugin?

programmeradmin1浏览0评论

I am experimenting with bulk import from csv file for my plugin. As I am new to plugin develpoment, please guide me if I am doing something wrong.

What I have:

I have 4 columns of multiple rows in a CSV file. Upon Importing on the admin backend on my plugin, I want 'All' those rows data to be inserted into rows of that particular table in mysql database.

What is the problem:

Only the 1st row is getting inserted. The rest are being ignored.

Here is the php code of my plugin:

<?php
    if($_FILES['csv_data']['name']){
        $arrFileName = explode('.',$_FILES['csv_data']['name']);
        if($arrFileName[1] == 'csv'){
        $handle = fopen($_FILES['csv_data']['tmp_name'], "r");
        while (($data = fgetcsv($handle, 100, ",")) !== FALSE) {

        $chkInId = $data[0];
        $chkOutId = $data[1];
        $chkInIsbn = $data[2];
        $booksReturn = $data[3];
        $wpdb->insert($tablename, array(
                'bf_chkin_id' =>$chkInId,
                'bf_chkout_id' =>$chkOutId,
                'bf_chkin_isbn' =>$chkInIsbn,
                'bf_chkin_bookreturn' => $booksReturn
              ));
        echo "Inserted";
        }
        fclose($handle);
        print "Import done";
        }
        }
?>
<!-- Form -->
<form method='post' action='<?= $_SERVER['REQUEST_URI']; ?>' enctype='multipart/form-data'>
  <input type="file" name="csv_data" >
  <input type='submit' name='submit' value='Upload File'>
</form>

I cannot find out what is the error. Please guide or educate me with a simple example if possible. Thank you in advance.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论