表单输入使用$ wpdb提交到数据库

时间:2014-12-23 07:01:49

标签: wordpress

我想知道如何将表单输入值插入到wordpress数据库中。 在wordpress主题中,我想添加一个输入元素并将其插入数据库。 单击提交按钮,我认为插入以下代码没有任何效果。

wordpress_theme_file.php

<form id=”form_reply” action=”database.php” method=”post”>
…
<input type=”text” id=”newValue” name=”newValue” />
…
</form>

database.php中

<?php
global $wpdb;
$inputValue = $_POST[‘ newValue ‘];
$wpdb->insert(‘wp_table_name’, ‘field_name’=>$inputValue);  // wp_table_name and field_name in database
?>

2 个答案:

答案 0 :(得分:0)

你可以像这样插入wordpress表

global $wpdb;
$inputValue = $_POST['newValue'];
$wpdb->insert( 
    'table_name', 
    array( 
        'column1' => $inputValue
    ), 
    array( 
        '%s' // if the field type is string
    ) 
);

for more info

答案 1 :(得分:0)

示例代码,可帮助您在wordpress中将数据插入数据库:

 <?php
     global $wpdb

    $address = 'address';
    $firstn = "First Name';
    $wpdb->insert( 'users', array( 'address' => $address, 'first_name' => $firstn ), array( '%s', '%s' ) )

 ?>

在上面&#34;用户&#34;是表名,字段是:地址和名字。

参考:http://codex.wordpress.org/Function_Reference/wpdb_Class#INSERT_rows

相关问题