简体   繁体   English

使用wpdb从CSV导入MySQL

[英]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. 我正在研究需要从CSV导入一些数据到MySQL的模块。 I'm using wpdb core library to do this. 我正在使用wpdb核心库来执行此操作。 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. 为此,我创建了一个名为upload_data()的函数,在其中传递$file如前所述声明的$file变量。

Function upload_data(): 函数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. 我认为这是由于$file没有通过函数。

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 ) 例如,将$officer_name更改$officer_name $name (或将$name更改$officer_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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