从Wordpress插件数据未插入自定义表

时间:2017-09-06 12:32:16

标签: 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";

    }

2 个答案:

答案 0 :(得分:1)

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

}

答案 1 :(得分:0)

经过大量的阅读和调试后,我才知道这个问题。我的cookie未正确保存。尽管它正在使用localhost,但它并没有在网站上运行。阅读此[PHP cannot read javascript cookies后,我在保存后修改我的cookie后。 我正在设置像这样的cookie而且它没有工作 -

document.cookie="cookiename="+value
设置完成后,

工作了 -

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