我有一個 wordPress 插件,我無法將資料添加到自定義資料庫表中。這是我到目前為止所嘗試的。
我也嘗試過插入一行,沒有插入任何內容,我在這里做錯了什么?感謝您花時間檢查。
<?php
global $wpdb;
// Table name
$tablename = $wpdb->prefix."bohio";
// Import CSV
if(isset($_POST['butimport'])){
// File extension
$extension = pathinfo($_FILES['import_file']['name'], PATHINFO_EXTENSION);
// If file extension is 'csv'
if(!empty($_FILES['import_file']['name']) && $extension == 'csv'){
$totalInserted = 0;
// Open file in read mode
$csvFile = fopen($_FILES['import_file']['tmp_name'], 'r');
fgetcsv($csvFile); // Skipping header row
// Read file
while(($csvData = fgetcsv($csvFile)) !== FALSE){
$csvData = array_map("utf8_encode", $csvData);
// Row column length
$dataLen = count($csvData);
// Skip row if length != 4
if( !($dataLen == 4) ) continue;
// Assign value to variables
$square_feet = trim($csvData[0]);
$bed = trim($csvData[1]);
$bath = trim($csvData[2]);
$bh_service = trim($csvData[3]);
$price = trim($csvData[4]);
// Check record already exists or not
$cntSQL = "SELECT count(*) as count FROM {$tablename} where price='".$price."'";
$record = $wpdb->get_results($cntSQL, OBJECT);
if($record[0]->count==0){
// Check if variable is empty or not
if(!empty($square_feet) && !empty($bed) && !empty($bath) && !empty($bh_service) && !empty($price) ) {
// Insert Record
$wpdb->insert($tablename, array(
'square_feet' =>$square_feet,
'bed' =>$bed,
'bath' =>$bath,
'bh_service' =>$bh_service,
'price' => $price
));
if($wpdb->insert_id > 0){
$totalInserted ;
}
}
}
}
echo "<h3 class='bohio_valid'>Total record Inserted : ".$totalInserted."</h3>";
}else{
echo "<h3 class='bohio_invalid'>Invalid File Extension</h3>";
}
}
?>
我從檔案中匯入的 CSV 資料在這里:
square_feet,bed,bath,bh_service,price
Under 1000,studio,1,Deep Clean,300.00
Under 1000,studio,1,Move In/Move Out,350.00
Under 1000,studio,1,Post Construction,375.00
1000-1600,1,1,Deep Clean,350.00
資料根本沒有被添加。
uj5u.com熱心網友回復:
解決了。我實際上有 5 個資料列,實際上我只允許 4 個。
// Skip row if length != 4
if( !($dataLen == 4) ) continue
將其更改為 5,它就像一個魅力。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/516315.html
