将表单数据存储到wordpress数据库

时间:2016-05-19 05:11:38

标签: php database wordpress

我创建了一个简单的html表单来测试,我如何在wpdb中插入数据。我知道如何在PHP中实现它,但在wordpress中我感到困惑!

global $wpdb;
if (isset($_POST['submit'])) {
    $name = $_POST['r1s1']; //Here r1s1 is name of the form control
    $email = $_POST['r2s1'];
    $contact = $_POST['r3s1'];
    $address = $_POST['r5s1'];
    if ($name != '' || $email != '') {
        $post = $wpdb->insert(
                'table', array(
                    'feild' => $name,
                    'dev' => $email,
                    'tenant' => $contact,
                    'mod' => $address,
                ), array(
                    '%s',
                    '%s',
                    '%s',
                    '%s'
                )
        );
        echo $post;
        if ($post) {
            echo "<br/><br/><span>Data Inserted successfully...!!</span>";
        } else {
            echo "<br/><br/><span>Insertion Failed...!!</span>";
        }
    } else {
        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}

当我点击提交按钮时,它会显示消息insertion failed...!!(我通过回信打印的消息)。此代码是用模板编写的。我做错了什么?

2 个答案:

答案 0 :(得分:2)

HttpURLConnection

试试这个

答案 1 :(得分:1)

您可以尝试以下代码吗?

global $wpdb;
$table_name = $wpdb->prefix . "your_table_name";
if (isset($_POST['submit'])) {
    $name = $_POST['r1s1']; //Here r1s1 is name of the form control
    $email = $_POST['r2s1'];
    $contact = $_POST['r3s1'];
    $address = $_POST['r5s1'];
    $data = array(
        'feild' => $name,
        'dev' => $email,
        'tenant' => $contact,
        'mod' => $address
    );

    if ($name != '' || $email != '') {
        $result = $wpdb->insert($table_name, $data);          
        if ($result) {
            echo "<br/><br/><span>Data Inserted successfully...!!</span>";
        } else {
            echo "<br/><br/><span>Insertion Failed...!!</span>";
        }
    } else {
        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}