这是我正在编写的一个函数,它应该将一个输入以及一些其他信息发布到数据库中。从现在起,激活插件会产生一个WSoD,当该函数被删除时,其余的插件就会按预期运行。
这是输入:
<input type="text" maxlength="4" name="\' . $quanid . \'" value="" class="input" />
<button class="submit" type="submit" value="Submit">Submit</button>
$quanid来自CSV文件:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$quanid = $data[2];
<小时>
add_action(\'login_head\',\'ref_access\');
function ref_access(){
global $error;
if (is_user_logged_in())
$newdb = new wpdb( \'user\', \'pass\', \'db\', \'localhost\' );
global $newdb;
$hf_username = wp_get_current_user();
$inputValue = $_POST[$quanid];
$wpdb->insert(
$table,
array(
\'ItemID\' => $quanid
\'Price\' => $inputValue
\'user\' => $hf_username
),
);
{
}else {
$error = "Error: You must be logged in to submit prices";
return ;
}
}
最合适的回答,由SO网友:Adam 整理而成
您的代码格式和语法至少有问题:
function ref_access(){
global $error;
if (is_user_logged_in()) // <-- problem here...
$newdb = new wpdb( \'user\', \'pass\', \'db\', \'localhost\' );
global $newdb;
$hf_username = wp_get_current_user();
$inputValue = $_POST[$quanid];
$wpdb->insert(
$table,
array(
\'ItemID\' => $quanid
\'Price\' => $inputValue
\'user\' => $hf_username
),
);
{ // <-- problem here...
}else {
$error = "Error: You must be logged in to submit prices";
return ;
}
}
格式正确:
function ref_access(){
global $error;
if ( is_user_logged_in() ) {
global $newdb;
//you are not using $newdb anywhere in this functions
$newdb = new wpdb( \'user\', \'pass\', \'db\', \'localhost\' );
$hf_username = wp_get_current_user();
$inputValue = $_POST[$quanid];
//you need to delcare global $wpdb here if you plan to use $wpdb instead of $newdb
$wpdb->insert(
$table, //there is no $table variable within this function
array(
\'ItemID\' => $quanid
\'Price\' => $inputValue
\'user\' => $hf_username
)
);
} else {
$error = "Error: You must be logged in to submit prices";
return;
}
}
更新:关于您对
$table
在另一个函数中定义后,需要传递值
$table
到函数调用
ref_action(\'my_table_name\')
.
示例:
function ref_access($table = \'\') {
//function code
}
//Usage
ref_access(\'my_table_name\');