I have created a simple html form to test, how I can insert data in wpdb. I know how to do it in PHP but in wordpress I am getting confused!
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>";
}
}
When I click on submit button then it shows message insertion failed...!!
( Message that I am printing via echo). This code is written in template. What I am doing wrong?