Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Have you added any prefix to newsletter table? –  Vinod Dalvi Mar 14 '13 at 13:20
    
adding prefix where exactly? the table in database have prefix –  pixelweb Mar 14 '13 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"; –  Vinod Dalvi Mar 14 '13 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). –  t f Mar 14 '13 at 13:37
    
@VinodDalvi : i added th eprefix for table in database. –  pixelweb Mar 14 '13 at 13:46
show 2 more comments

1 Answer

up vote 1 down vote accepted

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
) );
share|improve this answer
    
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. –  t f Mar 14 '13 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 –  Mike Madern Mar 14 '13 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. –  t f Mar 14 '13 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 Mar 14 '13 at 14:00
    
You do call the insertuser() function after you declare the function? –  Mike Madern Mar 14 '13 at 14:02
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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