这两个变量$name
和$email
函数内部未知。您必须通过更改global $wpdb
进入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();
或者,您可以将变量放入函数的参数中:
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 );
或者,无功能:
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
) );