I am writing a simple plugin that create a table with name “newsletter” in database and provide a shortcode to put a registration form in pages.
the form contain “name” and “email”.
i have problem with inserting the form data(name+email) in database.
i wrote this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
function insertuser(){
global $wpdb;
$table_name = $wpdb->prefix . "newsletter";
$wpdb->insert($table_name, array('name' => $name, 'email' => $email) );
}
?>
but id does not work. what should i do to get data from form and insert into table?
The two variables
$name
and$email
are unknown inside the function. You have to make them globally available inside it by changingglobal $wpdb
intoglobal $wpdb, $name, $email
:Or, you can put the variables in the function’s arguments:
Or, without function: