使用我的插件功能将表单数据放入我的数据库中

时间:2019-01-09 13:34:16

标签: php mysql wordpress

我正在为我的网站创建一个wp插件,并且我的网站上有一个表单,必须在数据库中添加一些价值。我的问题是数据不想放入我的数据库中。

我尝试多次更改编码,然后调试功能(数据通过功能可接收),但实际上我的代码如下:

我的首页:

$date_start = $_POST['startDate'];
$date_end = $_POST['endDate'];
$description = $_POST['description'];
$place = $_POST['lieu'];
$value = $_POST['value'];
$user_id = $_POST['id_user'];

$current_user = wp_get_current_user();
$current_user_id = $current_user->id;

$date_current = date('Y/m/d', time());

frdp_add($user_id, $description, $place, $date_start, $date_end, $value, $date_current, $current_user_id);

我的插件页面:

$table_name = $wpdb->prefix . 'frais_deplacement';

function frdp_add($user_id, $description, $place, $date_start, $date_end, $value, $date_current, $current_user_id) {
global $wpdb;

$wpdb->insert(
    $table_name,
    array(
        'id_user' => $user_id, 
        'description' => $description,
        'place' => $place,
        'date_start' => $date_start, 
        'date_end' => $date_end, 
        'value' => $value, 
        'created_at' => $date_current, 
        'created_by' => $current_user_id
    ) 
);  
}

2 个答案:

答案 0 :(得分:0)

您的代码应如下所示,您跳过了函数中的参数。

$table_name = $wpdb->prefix . 'frais_deplacement';

function frdp_add($current_user_id, $description, $place, $date_start, $date_end, $value, $date_current, $current_user_id) {
       global $wpdb;

       $wpdb->insert(
$table_name,
array(
    'id_user' => $current_user_id, 
    'description' => $description,
    'place' => $place,
    'date_start' => $date_start, 
    'date_end' => $date_end, 
    'value' => $value, 
    'created_at' => $date_current, 
    'created_by' => $current_user_id,
) 
 );  
}

答案 1 :(得分:0)

好吧,我不能将值放入数据库,因为表名不是通过函数来​​获得的。为此,我必须像这样将 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>R7 FrontEnd Test</title> <link href="css/style.css" rel="stylesheet" type="text/css"> </head> <body> <!-- let's code :D --> <div class="container"> <div class="box"> <div class="box-head"> <span class="logo"> <img src="./assets/a-fazenda-logo.png" alt=""> </span> <span class="title"> <h1>RANKING</h1> </span> </div> <div id="ranking" class="main-box"> </div> </div> </div> <script src="./javascripts/js.js"></script> </html> 插入我的函数中:

global $table_name;