如何使用php和mysql代码在wordpress数据库中插入表单数据

时间:2016-02-08 19:42:56

标签: php mysql wordpress

我试图从表单中插入wp数据库中的一些值,但它没有插入。非常感谢你的帮助。

<form method="post" action=""> 
Name: <input type="text" name="name" id="name" /> 
Email: <input type="text" name="email" id="email"/> 
<input type="submit" name="submit"/> 
</form>




<?php 
global $wpdb; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$table_name = $wpdb->prefix . "user2"; 
$wpdb->insert( $table_name, array(
        'name' => $name,
        'email' => $email ) ); ?>

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码插入:

<form method="post" action=""> 
Name: <input type="text" name="name" id="name" /> 
Email: <input type="text" name="email" id="email"/> 
<input type="submit" name="submit"/> 
</form>

<?php 
if(isset($_POST['submit'])){
global $wpdb; 
$name = $_POST['name']; 
$email = $_POST['email'];
if(isset($_POST['name ']) && isset($_POST['email '])) {
$table_name = $wpdb->prefix . "user2"; 
$wpdb->insert( $table_name, array(
        'name' => $name,
        'email' => $email ) );
}
else{
echo "No parameter";exit;
}
} ?>

或者您可以尝试核心php

<form method="post" action=""> 
Name: <input type="text" name="name" id="name" /> 
Email: <input type="text" name="email" id="email"/> 
<input type="submit" name="submit"/> 
</form>

<?php 
if(isset($_POST['submit'])){
global $wpdb; 
$name = $_POST['name']; 
$email = $_POST['email'];

$table_name = $wpdb->prefix . "user2"; 

        $sql="insert into '$table_name' table (name,email)values('$name','$email')";
        mysql_query($sql);


} ?>