Kunal Dholiya
Kunal Dholiya

Reputation: 95

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.

Upvotes: 1

Views: 704

Answers (2)

Kunal Dholiya
Kunal Dholiya

Reputation: 95

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 );
        }
    }

Upvotes: 2

Vos
Vos

Reputation: 114

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. F.e. change $officer_name to $name (or $name to $officer_name )

Upvotes: 0

Related Questions