I know this is duplicate question. But I have tried all the answers which I have found on the http://stackoverflow.com/ .

I think in my case I am inserting and updating data if same date match than record will update if not than it will insert.

Below is my file code:

<?php 
if ( isset( $_POST['submit'] ) && $_POST['upload-csv'] == 'upload' ) {
    $error = array();
    $success = array();
    $filename = $_FILES['file']['name'];
    $filetype = wp_check_filetype( $filename );

    if ( $filetype['ext'] == 'csv' && $filetype['type'] == 'text/csv' ) {

        $handle = fopen( $_FILES['file']['tmp_name'], "r" );
        $row = 0;
        $skip_row_number = array("1");
        while ( ($data = fgetcsv( $handle, 1000, "," )) !== FALSE ) {
            $data = array_map("utf8_encode", $data);

            if ($row > 0)   
            {

                $table_name = $wpdb->prefix . 'prayer';
                $ipquery = $wpdb->get_results("SELECT * FROM `$table_name` WHERE `date` = '".$data[0]."'");     
                $query_res = $wpdb->num_rows;   
                // Check if same date data 
                if($query_res >=1){
                    $updateQuery = "UPDATE `$table_name` SET 
                                            `date` = '".$data[0]."', 
                                            `first_start` = '".$data[1]."',
                                            `first_end` = '".$data[2]."', 
                                            `second_start` = '".$data[3]."', 
                                            `second_end` = '".$data[4]."', 
                                            `third_start` = '".$data[5]."', 
                                            `third_end` = '".$data[6]."', 
                                            `forth_start` = '".$data[7]."', 
                                            `forth_end` = '".$data[8]."', 
                                            `five_start` = '".$data[9]."', 
                                            `five_end` = '".$data[10]."', 
                                            `done` = '".$data[10]."'
                                            WHERE `$table_name`.`date` = '".$data[0]."';";
                    $up_res = $wpdb->query($updateQuery);
                }else{
                    $query = "INSERT INTO $table_name (date, first_start, first_end, second_start,
                             second_end, third_start, third_end, forth_start, forth_end, five_start, five_end, done) 
                             VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."','".$data[5]."','".$data[6]."','".$data[7]."','".$data[8]."','".$data[9]."','".$data[10]."','".$data[11]."')";
                    $insert_res = $wpdb->query($query);
                }

            }

        $row++; 
        }
        fclose( $handle );
        $success[] = 'Import done.';
    } else {
        $error[] = 'Please upload CSV file only';
    }
}   
?>

I have tried the below answer for skip the header:

Skip the first line of a CSV file

Import CSV, exclude first row

skip first line of fgetcsv method in php

Help me sort out this issue.

Read more here: When import CSV skip header or first row


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: