使用
-
-
您是否在新闻通讯表中添加了任何前缀?Have you added any prefix to newsletter table?
- 0
- 2013-03-14
- Vinod Dalvi
-
到底在哪里添加前缀?数据库中的表具有前缀adding prefix where exactly? the table in database have prefix
- 0
- 2013-03-14
- pixelweb
-
在表名之前,因为您已经使用了$table_name=$ wpdb->prefix."通讯";在您的代码中,在表名新闻稿之前添加了wordpress前缀,因此,如果您没有向表名添加任何前缀,则只能使用像这样的表名$table_name="newsletter";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";
- 0
- 2013-03-14
- Vinod Dalvi
-
函数内部的两个变量`name`和`email`是** unknown **.您必须在函数内部定义它们,或者如果在其他地方需要它们,则将它们声明为"global"(函数的_outside_和_inside_).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).
- 0
- 2013-03-14
- tfrommen
-
@VinodDalvi:我在数据库中添加了表的前缀.@VinodDalvi : i added th eprefix for table in database.
- 0
- 2013-03-14
- pixelweb
-
我定义了内部函数的名称和电子邮件,但没有任何反应.i defined the name and email inside function but nothing happen.
- 0
- 2013-03-14
- pixelweb
-
您添加到表中的前缀是什么?告诉我带有前缀的完整表名.What is the prefix you have added to the table? tell me the full table name with prefix.
- 0
- 2013-03-15
- Vinod Dalvi
-
1 个回答
- 投票数
-
- 2013-03-14
函数内部两个变量
使它们在全局范围内可用$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 ) );
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
: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 ) );
-
那就是我写的;)关于何时撰写评论以及何时足以回答问题,我仍然有疑问.;)变量仍然必须声明为`global` _outside_函数.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.
- 0
- 2013-03-14
- tfrommen
-
哈哈,是的,我在发布答案后看到了您的评论:-)我的评论/回答规则是,如果OP必须在代码中更改多个规则,请务必回答;-)我将在变量$name和$emailHaha, 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`
- 1
- 2013-03-14
- Mike Madern
-
嗯,好吧,您似乎对范围是正确的(因为在这种情况下,函数_outside_是全局范围,而不是类).但是,如果将变量声明为全局变量(现在决定要做什么),则必须先声明,然后(在下一行或分号后)设置一个值.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.
- 0
- 2013-03-14
- tfrommen
-
当用户单击提交表单时,表单的动作引用该文件中的文件调用:regiostration-form.php,我现在具有以下代码: `前缀."通讯"; $ wpdb->insert($table_name,array('name'=> $name,'email'=> $email));;复制代码 } ?>` 但它不再起作用.哪里不对了?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: `prefix . "newsletter"; $wpdb->insert($table_name, array('name' => $name, 'email' => $email) ); } ?> ` but it does not work again. anything wrong?
- 0
- 2013-03-14
- pixelweb
-
声明函数后,您是否要调用`insertuser()`函数?You do call the `insertuser()` function after you declare the function?
- 0
- 2013-03-14
- Mike Madern
-
@MikeMadern我是否必须在函数后编写:'insertuser()'?@MikeMadern do i have to write: 'insertuser()' after function?
- 0
- 2013-03-14
- pixelweb
-
定义函数不会自动执行它.您必须调用该函数才能执行.看到我的答案中的代码;-)defining a function doesn't automatically executes it. You have to call the function in order to execute. See the code in my answer ;-)
- 0
- 2013-03-14
- Mike Madern
-
我使用了您的代码,但遇到两个错误:注意:"试图在第8行中获取非对象的属性"和"致命错误:在第9行中的非对象上调用成员函数insert()"i used your code but i got two errors: Notice: `Trying to get property of non-object in line 8` and `Fatal error: Call to a member function insert() on a non-object in line 9`
- 0
- 2013-03-14
- pixelweb
-
这不是WordPress加载的文件吗?在脚本顶部`require``wp-load.php`文件以加载WordPress库.It isn't a file loaded by WordPress right? `require` the `wp-load.php` file on top of your script to load the WordPress library.
- 0
- 2013-03-14
- Mike Madern
-
我使用此代码并正常工作:`前缀."通讯"; $ wpdb->insert($table_name,array('name'=> $name,'email'=> $email)));复制代码 ?> `特别感谢所有人Mike Maderni used this code and worked fine: `prefix . "newsletter"; $wpdb->insert( $table_name, array( 'name' => $name, 'email' => $email ) ); ?> ` Thanks all guys specially Mike Madern
- 0
- 2013-03-14
- pixelweb
-
是的,请用完整答案更新答案,然后我会接受.真诚的yes, please update the answer with complete answer then i will accept it. Sincererly
- 0
- 2013-03-14
- pixelweb
-
我为您更新了答案;)I updated my answer for you ;)
- 0
- 2013-03-15
- Mike Madern
我正在编写一个简单的插件,该插件在数据库中创建一个名称为"newsletter"的表,并提供一个短代码以在页面中放置注册表单. 表单包含"名称"和"电子邮件". 我在数据库中插入表格数据(名称+电子邮件)时遇到问题. 我写了这个:
,但ID不起作用.我应该怎么做才能从表单中获取数据并将其插入表中?