从WordPress插件数据中没有插入自定义表中



我创建了一个插件来插入和获取记录。当我保存记录时,记录将从Localhost中保存下来,但是当我在在线服务器上尝试它时,它并没有保存。这是我要做的。

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";
    }
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();
}

在阅读和调试后,我知道了这个问题。我的饼干没有正确保存。即使它在Localhost上工作,它也没有在网站上工作。阅读此[PHP无法阅读JavaScript cookie,然后在保存时修改了饼干后。我是这样设置的cookie,它没有工作 -

document.cookie="cookiename="+value

设置后,它可以工作 -

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

最新更新