2016-01-27 75 views
-1

當我嘗試從我的函數添加記錄時遇到此錯誤有任何解決此問題的建議?將記錄添加到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'; 
     $result=$wpdb->insert(
      $table_name, 
      array('email'=>$set_mail 
      ) 
     ); 
} 

*編輯我加入我的整個PHP代碼由於request.here我發送一個鏈接,併爲我保持電子郵件發送我的鏈接作爲記錄。

function sendmail() 
{ 
    $email = $_POST['email']; 
    $passmail=md5($email); 
    $subject = 'The Registration Link'; 
    $headers = "From: [email protected] ". "\r\n"; 
    $headers .= "Reply-To: [email protected]" . "\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(); 

} 

所有函數都在同一個文件中。

+0

刪除逗號'email'=> $ set_mail在你的數組內傳遞給插入 –

+0

對不起,當我在這裏添加代碼時出錯了.re編輯了這個問題。我是否需要包含任何文件? –

+0

你已經將$ wpdb聲明爲全局的,所以它不應該拋出這個錯誤。對我來說很陌生。 –

回答

0

試試這個

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

Call setdb('[email protected]'); 
+0

這是我寫的函數添加一個記錄到我的分貝時,我發送電子郵件給用戶我在這個函數中調用了函數,我需要發送像setdb($ passmail)這樣的郵件;它可以嗎?這兩個函數都在同一個php中,並且郵件函數在郵件地址是Post.if(isset($ _POST ['email'])){sendmail();} –

0

好吧,我解決了這個通過直接調用WordPress的配置文件,並使用默認mysqli commands.I嘗試包括WP-db.php中和得到這個工作,但沒有奏效。