php - Error in Inserting csv records to mysql table in wordpress -
i'm having problem on how can insert csv file selected table,i want insert csv records other columns blank. want insert records having duplicates here code:
// if fields input; , file correct .csv format; continue if(!empty($_post['table_select']) && !empty($_post['csv_file']) && ($ext === 'csv')) { // grab columns $db_cols = $wpdb->get_col( "desc " . $_post['table_select'], 0 ); // array of db column names // number of columns hidden input field (re-auto-populated via jquery) $numcolumns = $_post['num_cols']; // open .csv file , it's contents if(( $fh = @fopen($_post['csv_file'], 'r')) !== false) { // set variables $values = array(); $too_many = ''; // used alert users if columns not match while(( $row = fgetcsv($fh)) !== false) { // file contents , set row array if(count($row) == $numcolumns) { // if .csv column count matches db column count $values[] = '("' . implode('", "', $row) . '")'; // each new line of .csv file becomes array } } // set staring row number 2(two) prevent adding column header titles of csv file $num_var = '2'- 1; // subtract 1 make counting easy on non-techie folk! (1 0 in binary) // // if user input number exceeds available .csv rows // if($num_var > count($values)) { // $error_message .= '* '.__('starting row value exceeds number of entries being updated database .csv file.','jwp_csv_2_db').'<br />'; // $too_many = 'true'; // set alert variable // } // // else splice array , remove number (rows) user selected // else { $values = array_slice($values, $num_var); // } // if user did not input more rows available .csv file if($too_many !== 'true') { //$db_query_update = ''; $db_query_insert = ''; // format $db_cols string $db_cols_implode = implode(',', $db_cols); // format $values string $values_implode = implode(',', $values); // sql query insert csv data databas table $sql = 'insert '.$_post['table_select'] . ' (' . $ . ') ' . 'values ' . $values_implode; $db_query_insert = $wpdb->query($sql); // // if db db_query_update successful // if ($db_query_update) { // $success_message = __('congratulations! database has been updated successfully.','jwp_csv_2_db'); // } // // if db db_query_insert successful //elseif ($db_query_insert) { if ($db_query_insert) { $success_message = __('congratulations! database has been updated successfully.','jwp_csv_2_db'); $success_message .= '<br /><strong>'.count($values).'</strong> '.__('record(s) inserted the', 'jwp_csv_2_db').' <strong>'.$_post['table_select'].'</strong> '.__('database table.','jwp_csv_2_db'); } // // if db db_query_insert successful , there no rows udpate // elseif( ($db_query_update === 0) && ($db_query_insert === '') ) { // $message_info_style .= '* '.__('there no rows update. .csv values exist in database.','jwp_csv_2_db').'<br />'; // } else { $error_message .= '* '.__('there problem database query.','jwp_csv_2_db').'<br />'; echo mysql_errno() . ": " . mysql_error() . "\n"; $error_message .= '* '.__('a duplicate entry found in database .csv file entry.','jwp_csv_2_db').'<br />'; } } } else { $error_message .= '* '.__('no valid .csv file found @ specified url. please check "select csv file" field , ensure points valid .csv file.','jwp_csv_2_db').'<br />'; } }
Comments
Post a Comment