vivek kumar
vivek kumar

Reputation: 55

From Wordpress plugin data is not getting inserted into custom table

I have created a plugin to insert and fetch records. When I save record, record is getting saved from localhost but when I try it on online server it is not getting saved. Here is what I am trying to do.

function data_custom_ajax(){      
        global $wpdb;
        $tbl = "tripplan";
        $table_name=$wpdb->prefix . $tbl;
        $custom_val = $_POST['text'];
        $totaltime = $_COOKIE['totalduration'];
        $totaldistance = $_COOKIE['totaldistance'];
        $origin_address = $custom_val['originaddress'];
        $end_address = $custom_val['destinationaddress'];
        $waypoints = $custom_val['waypts'];
          $wpdb->insert($table_name,
                          array(
                            'startpoint' => $origin_address,
                            'endpoint' => $end_address,
                            'waypoints' => json_encode($waypoints),
                            'totaldistance' => $totaldistance,
                            'totalduration' => $totaltime
                            ),
                            array('%s','%s','%s','%f','%f')
                          );
          echo "data has been saved";

    }

Upvotes: 1

Views: 115

Answers (2)

vivek kumar
vivek kumar

Reputation: 55

After lot of reading and debugging I got to know the problem. My cookie was not getting saved correctly. Even though it was working on localhost, it was not working on website. After reading this [PHP cannot read javascript cookies and then after I modified my cookie while saving. I was setting cookie like this and it was not working-

document.cookie="cookiename="+value

after setting this way it worked -

document.cookie = 'cookiename='+value+'; path=/'

Upvotes: 0

Akshay Shah
Akshay Shah

Reputation: 3504

function data_custom_ajax(){      
    global $wpdb;
    $tbl = "tripplan";
    $table_name=$wpdb->prefix . $tbl;
    $custom_val = $_POST['text'];
    $totaltime = $_COOKIE['totalduration'];
    $totaldistance = $_COOKIE['totaldistance'];
    $origin_address = $custom_val['originaddress'];
    $end_address = $custom_val['destinationaddress'];
    $waypoints = $custom_val['waypts'];

    $data = array(
        'startpoint' => $origin_address,
        'endpoint' => $end_address,
        'waypoints' => json_encode($waypoints),
        'totaldistance' => $totaldistance,
        'totalduration' => $totaltime
        )

    $lastInsertedId = $wpdb->insert($table_name,$data);


     if($lastInsertedId != '')
     {
        echo "data has been saved";
     }else{
         $wpdb->print_error();
     }
      die();

}

Upvotes: 1

Related Questions