简体   繁体   中英

Import from CSV to MySQL using wpdb

I'm working on the module in which I need to import some data from CSV to MySQL. I'm using wpdb core library to do this. The code which I've tried is as below:

 if(isset($_POST['upload'])){ $file = $_FILES['chooseFile']['name']; $varA->upload_data($file); } <form method="post" enctype="multipart/form-data"> <div class="file-upload"> <div class="file-select"> <div class="file-select-button" id="fileName">Choose File</div> <div class="file-select-name" id="noFile">No file chosen...</div> <input type="file" name="chooseFile" id="chooseFile"> </div> </div> <div class="form-group" style="margin-top: 10px;"> <input type="submit" name="upload" class="form-control btn-warning"> </div> </form> 

To do this I've created a function called upload_data() in which I'm passing $file variable declared as mentioned.

Function upload_data():

 public function upload_data($file){ global $wpdb; $file_data = $_FILES['chooseFile']['tmp_name']; $handle = fopen($file, "r"); $c = 0; while(($filesop = fgetcsv($handle, 1000, ",")) !== false) { $name = $filesop[0]; $contact = $filesop[1]; $adhar = $filesop[2]; $address = $filesop[3]; $reg = $filesop[4]; $data = array( 'name' => $officer_name, 'contact' => $officer_contact, 'adhar' => $officer_adhar, 'address' => $officer_address, 'reg' => $officer_reg, ); $wpdb->insert( 'data' , $data ); } } 

This function is not working. I think this is due to $file not passing through the function.

I've got the solution for this. Thank you for your contribution.

 if(isset($_POST['upload'])){ $file = $_FILES['chooseFile']['name']; $file_data = $_FILES['chooseFile']['tmp_name']; $handle = fopen($file_data, "r"); $c = 0; while(($filesop = fgetcsv($handle, 1000, ",")) !== false){ $name = $filesop[0]; $contact = $filesop[1]; $adhar = $filesop[2]; $address = $filesop[3]; $reg = $filesop[4]; $data = array( 'name' => $name, 'contact' => $contact, 'adhar' => $adhar, 'address' => $address, 'reg' => $reg, ); $wpdb->insert( 'data' , $data ); } } 

You are setting variables like:

$name = $filesop[0];
            $contact = $filesop[1];
            $adhar = $filesop[2];
            $address = $filesop[3];
            $reg = $filesop[4];

But after that, when storing, you use different variable names? See:

$data = array(
                'name' => $officer_name,
                'contact' => $officer_contact,
                'adhar' => $officer_adhar,
                'address' => $officer_address,
                'reg' => $officer_reg,
            );

            $wpdb->insert( 'data' , $data );

I don't have the full scope of the code so maybe it is intended? But otherwise I think that this is your problem. Fe change $officer_name to $name (or $name to $officer_name )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM