I’m having problem on how can I insert the csv file to the selected table,I want to insert those csv records even the other columns are blank.
I want to insert its records even its having duplicates
Here is my code:
// If all fields are input; and file is correct .csv format; continue
if(!empty($_POST['table_select']) && !empty($_POST['csv_file']) && ($ext === 'csv')) {
// Grab all columns
$db_cols = $wpdb->get_col( "DESC " . $_POST['table_select'], 0 ); // Array of db column names
// Get the number of columns from the hidden input field (re-auto-populated via jquery)
$numColumns = $_POST['num_cols'];
// Open the .csv file and get it's contents
if(( $fh = @fopen($_POST['csv_file'], 'r')) !== FALSE) {
// Set variables
$values = array();
$too_many = ''; // Used to alert users if columns do not match
while(( $row = fgetcsv($fh)) !== FALSE) { // Get file contents and set up row array
if(count($row) == $numColumns) { // If .csv column count matches db column count
$values[] = '("' . implode('", "', $row) . '")'; // Each new line of .csv file becomes an array
}
}
// Set the staring row number to 2(two) to prevent adding the column header titles of the csv file
$num_var = '2'- 1; // Subtract one to make counting easy on the non-techie folk! (1 is actually 0 in binary)
// // If user input number exceeds available .csv rows
// if($num_var > count($values)) {
// $error_message .= '* '.__('Starting Row value exceeds the number of entries being updated to the database from the .csv file.','jwp_csv_2_db').'<br />';
// $too_many = 'true'; // set alert variable
// }
// // Else splice array and remove number (rows) user selected
// else {
$values = array_slice($values, $num_var);
// }
// If the user DID NOT input more rows than are available from the .csv file
if($too_many !== 'true') {
//$db_query_update = '';
$db_query_insert = '';
// Format $db_cols to a string
$db_cols_implode = implode(',', $db_cols);
// Format $values to a string
$values_implode = implode(',', $values);
// SQL Query to Insert the csv data to the databas table
$sql = 'INSERT INTO '.$_POST['table_select'] . ' (' . $ . ') ' . 'VALUES ' . $values_implode;
$db_query_insert = $wpdb->query($sql);
// // If db db_query_update is successful
// if ($db_query_update) {
// $success_message = __('Congratulations! The database has been updated successfully.','jwp_csv_2_db');
// }
// // If db db_query_insert is successful
//elseif ($db_query_insert) {
if ($db_query_insert) {
$success_message = __('Congratulations! The database has been updated successfully.','jwp_csv_2_db');
$success_message .= '<br /><strong>'.count($values).'</strong> '.__('record(s) were inserted into the', 'jwp_csv_2_db').' <strong>'.$_POST['table_select'].'</strong> '.__('database table.','jwp_csv_2_db');
}
// // If db db_query_insert is successful AND there were no rows to udpate
// elseif( ($db_query_update === 0) && ($db_query_insert === '') ) {
// $message_info_style .= '* '.__('There were no rows to update. All .csv values already exist in the database.','jwp_csv_2_db').'<br />';
// }
else {
$error_message .= '* '.__('There was a problem with the database query.','jwp_csv_2_db').'<br />';
echo mysql_errno() . ": " . mysql_error() . "n";
$error_message .= '* '.__('A duplicate entry was found in the database for a .csv file entry.','jwp_csv_2_db').'<br />';
}
}
}
else {
$error_message .= '* '.__('No valid .csv file was found at the specified url. Please check the "Select CSV File" field and ensure it points to a valid .csv file.','jwp_csv_2_db').'<br />';
}
}