6

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?

7
  • Have you added any prefix to newsletter table? Commented Mar 14, 2013 at 13:20
  • adding prefix where exactly? the table in database have prefix
    – pixelweb
    Commented Mar 14, 2013 at 13:22
  • before table name because you have used this $table_name = $wpdb->prefix . "newsletter"; in your code which adds wordpress prefix before the table name newsletter so if you have not added any prefix to table name than only use table name like this $table_name = "newsletter"; Commented Mar 14, 2013 at 13:35
  • The two variables name and email are unknown inside the function. You have to either define them inside the function, or if they are needed elsewhere, declare them global (both outside and inside the function).
    – tfrommen
    Commented Mar 14, 2013 at 13:37
  • @VinodDalvi : i added th eprefix for table in database.
    – pixelweb
    Commented Mar 14, 2013 at 13:46

1 Answer 1

7

The two variables $name and $email are unknown inside the function. You have to make them globally available inside it by changing global $wpdb into global $wpdb, $name, $email:

require_once('../../../wp-load.php');

/**
 * After t f's comment about putting global before the variable.
 * Not necessary (http://php.net/manual/en/language.variables.scope.php)
 */
global $name = $_POST['name'];
global $email = $_POST['email'];

function insertuser(){
  global $wpdb, $name, $email;
  $table_name = $wpdb->prefix . "newsletter";
  $wpdb->insert($table_name, array('name' => $name, 'email' => $email) ); 
}

insertuser();

Or, you can put the variables in the function's arguments:

require_once('../../../wp-load.php');

$name = $_POST['name'];
$email = $_POST['email']

function insertuser( $name, $email ) {
  global $wpdb;

  $table_name = $wpdb->prefix . 'newsletter';
  $wpdb->insert( $table_name, array(
    'name' => $name,
    'email' => $email
  ) );
}

insertuser( $name, $email );

Or, without function:

require_once('../../../wp-load.php');

global $wpdb;

$name = $_POST['name'];
$email = $_POST['email'];
$table_name = $wpdb->prefix . "newsletter";
$wpdb->insert( $table_name, array(
    'name' => $name,
    'email' => $email
) );
12
  • That's what I wrote. ;) I'm still having issues with when to write a comment and when it's enough for an answer. ;) The variables still have to be declared global outside the function, though.
    – tfrommen
    Commented Mar 14, 2013 at 13:40
  • 1
    Haha, yes I saw your comment after I posted my answer :-) my rule of commenting/answering is, if OP has to change more than one rule in the code, always answer ;-) I'll add global to the variables $name and $email Commented Mar 14, 2013 at 13:44
  • Ah, okay, you seem to be right with the scope (because in this case, outside the function is the global scope, and not a class). However, if you declare the variables global (what you decided to do now), you first have to declare, and then (in the next line, or after a semicolon) set a value.
    – tfrommen
    Commented Mar 14, 2013 at 13:55
  • when user click on submit form the form's action refer to an file call :regiostration-form.php in this file i have this code now: <?php $name = $_POST['name']; $email = $_POST['email']; function insertuser(){ global $wpdb, $name, $email; $table_name = $wpdb->prefix . "newsletter"; $wpdb->insert($table_name, array('name' => $name, 'email' => $email) ); } ?> but it does not work again. anything wrong?
    – pixelweb
    Commented Mar 14, 2013 at 14:00
  • You do call the insertuser() function after you declare the function? Commented Mar 14, 2013 at 14:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.