WordPress:使用$wpdb将数据添加到自定义表中


wordpress: adding data to a custom table using $wpdb

我想使用以下内容将表单数据添加到自定义表中:

表名:wp_auctions

表字段 : first_name, last_name

文件registration_form.php是注册表。文件registration.php是旨在将数据添加到名为 wp_auctions 的数据库表的文件。

问题是提交表单后数据未添加到表中。有什么帮助吗?我做错了什么?

看起来$wpdb->insert()无法运行。

以下是我的两个文件:

文件: Registration_form.php

<?php
/*
template name: Registration_form;
Author: Aboubacar DRAME
*/
?>
<?php 
$directory=get_stylesheet_directory_uri(); 
?>
<form method= "POST", action=<?php echo $directory. "/registration.php";  ?>>
    <label for="first_name"> First Name </label>
    <input type="text" id="first_name" name="first_name"/> </br>
    <input type="submit" name="submit" value="submit the form" /> 
</form>

文件:注册.php

<?php
function Insert_data(){
global $wpdb;
$first_name = $_POST['first_name'];
$last_name  = $_POST['last_name'];
$wpdb->insert('wp_auctions', 
        array('first_name' => $first_name), 
               'last_name' => $last_name),
        array('first_name' => '%s',
               'last_name' => '%s')
     );
}
if(isset($_POST['submit']))
{
Insert_data();
} 
?>

$wpdb->insert代码不正确,应该是

$wpdb->insert(
       'wp_auctions', 
        array('first_name' => $first_name, 
               'last_name' => $last_name
        ),
        array( '%s',
               '%s'
        )
     );

更多信息见法典