Safin February 2016

How to insert form data in wordpress database using php and mysql code

I am trying to insert some values inside the wp database from the form but it's not getting inserted. your help would be appreciated very much.

<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 ) ); ?>

Answers


Vasim Vanzara February 2016

You can insert using this code :

<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;
}
} ?>

OR You can try core 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);


} ?>

Post Status

Asked in February 2016
Viewed 1,231 times
Voted 13
Answered 1 times

Search




Leave an answer