我正在处理需要从CSV导入一些数据到MySQL的模块。我正在使用wpdb
核心库来做到这一点。我尝试过的代码如下:
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>
为此,我创建了一个名为upload_data()
的函数,在其中传递了如上所述的声明的$file
变量。
函数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 );
}
}
此功能不起作用。我认为这是由于$file
没有通过该函数。
答案 0 :(得分:0)
您正在设置变量,例如:
$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 );
我没有完整的代码范围,所以也许是故意的?但是否则我认为这是您的问题。 F.e.将$officer_name
更改为$name
(或将$name
更改为$officer_name
)
答案 1 :(得分:0)
我已经找到了解决方案。谢谢您的贡献。
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 );
}
}