Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将记录添加到WordPress表时出现致命错误_Php_Wordpress - Fatal编程技术网

Php 将记录添加到WordPress表时出现致命错误

Php 将记录添加到WordPress表时出现致命错误,php,wordpress,Php,Wordpress,当我尝试从函数中添加记录时,出现此错误。有任何解决此问题的建议吗 PHP Fatal error: Call to a member function insert() on a non-object 我的职能是 function setdb($email){ global $wpdb; $set_mail=$email; $table_name = $wpdb->prefix . 'nd_tempemails';

当我尝试从函数中添加记录时,出现此错误。有任何解决此问题的建议吗

PHP Fatal error:  Call to a member function insert() on a non-object
我的职能是

function setdb($email){
        global $wpdb;           
        $set_mail=$email;
        $table_name = $wpdb->prefix . 'nd_tempemails';
        $result=$wpdb->insert(
            $table_name,
            array('email'=>$set_mail
            )
        );
}
*编辑由于请求,我添加了我的全部php代码。在这里,我发送了一个链接,为此,我保留了我发送的链接作为记录的电子邮件

function sendmail()
{
    $email = $_POST['email'];
    $passmail=md5($email);
    $subject = 'The Registration Link';
    $headers = "From: xxx@xxx.xx ". "\r\n";
    $headers .= "Reply-To: xxx@xxx.xx" . "\r\n".    
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n".'X-Mailer: PHP/' . phpversion();  
    $message = 'Here is your One time URL<br>';
    $message.="<a href='link";
    $message.=$passmail;
    $message.="'>click here</a>";
    mail($email, $subject, $message, $headers);
    setdb($passmail);
}
if(isset($_POST['email']))
{
    sendmail();

} 
函数sendmail() { $email=$_POST['email']; $passmail=md5($email); $subject='注册链接'; $headers=“From:xxx@xxx.xx“\r\n”; $headers.=“回复:xxx@xxx.xx“\r\n”。 $headers.=“MIME版本:1.0\r\n”; $headers.=“内容类型:text/html;charset=ISO-8859-1\r\n”。'X-Mailer:PHP/'.phpversion(); $message='这是您的一次性URL
'; $message.=“”; 邮件($email、$subject、$message、$headers); setdb($passmail); } 如果(isset($_POST['email'])) { sendmail(); } 所有函数都在同一个文件中。

试试这个

function setdb($email){
        global $wpdb;       
        $table_name = $wpdb->prefix.'nd_tempemails';
        $result=$wpdb->insert(
            $table_name,
            array('email'=>$email)
        );
}

Call setdb('xyz@xyz.com');

好的,我通过直接调用wordpress配置文件并使用默认的
mysqli
命令解决了这个问题。我试图包括wp-db.php并完成了这项工作,但没有成功。

在“email”=>$set\u传递给InsertMail的数组中删除逗号对不起,我在此处添加代码时出错。重新编辑了问题。我需要将任何文件包括到这??您已将$wpdb声明为全局,因此它不应抛出此错误。对我来说很奇怪。看起来还行(正如@mahadev所说,您已经将
$wpdb
声明为全局)。如何调用该函数?这是我编写的一个函数,用于在向用户发送电子邮件时向数据库中添加记录,因此我在需要发送邮件的函数中调用该函数,如setdb($passmail);可以吗?这两个函数都在同一个php中,邮件地址作为Post传递时会触发mail函数。if(isset($_Post['email']){sendmail();}这是我编写的一个函数,用于在向用户发送电子邮件时将记录添加到数据库中,因此我在函数中调用此函数,以便像setdb($passmail)一样发送邮件;可以吗?这两个函数在相同的php中,邮件地址作为Post传递时会触发mail函数