2013-09-27 35 views
0

我試圖理解並獲得$ wpdb全局對象,但似乎無法使其工作。基本上,我試圖在wordpress數據庫的一個自定義表格中插入一條記錄。我沒有看到創建自己的連接字符串,而是使用它,我已經看到Global $ wpdb對象爲我們做了訣竅。我遵循了Codex的例子,並試圖自己做,但失敗了。我沒有收到任何錯誤,但該記錄也沒有插入。幾個小時來解決這個問題。您的幫助將不勝感激。提前致謝。

代碼:

<?php 
class Listings{ 

    private $buisnessName; 
    private $contactName; 
    private $categories; 
    private $websiteURL; 
    private $telephoneNum; 
    private $address; 
    private $socialLinks; 
    private $shortDescription; 
    private $images; 
    private $fullDescription; 
    private $tags; 

    function __construct($buisnessName, $contactName, $categories, $websiteURL, $telephoneNum,$address,$socialLinks, $shortDescription, $images, $fullDescription,$tags){ 

     $this->buisnessName = $buisnessName; 
     $this->contactName = $contactName; 
     $this->categories = $categories; 
     $this->websiteURL = $websiteURL; 
     $this->telephoneNum = $telephoneNum; 
     $this->address = $address; 
     $this->socialLinks = $socialLinks; 
     $this->shortDescription = $shortDescription; 
     $this->images = $images; 
     $this->fullDescription = $fullDescription; 
     $this->tags = $tags;   
    } 

    function Insert_Listing(){ 
     global $wpdb; 
     $wpdb->insert('wp_listings_info', array("Buisness_Name" => $this->buisnessName,"Contact_Name" => $this->contactName, "Categories" => $this->categories, "Website_URL" => $this->websiteURL, "Telephone_Number" => $this->telephoneNum, "Address" => $this->address, "Social_Network_Links" => $this->socialLinks, "Short_Description" => $this->shortDescription, "Images" => $this->images, "Full_Description" => $this->fullDescription, "Tags" => $this->tags), array("%s", "%s","%s", "%s","%s", "%s","%s", "%s","%s", "%s","%s")); 
    } 

} 
?> 

表結構:

CREATE TABLE IF NOT EXISTS `wp_listings_info` (
`L_ID` int(11) NOT NULL AUTO_INCREMENT, 
`Business_Name` varchar(100) NOT NULL, 
`Contact_Name` varchar(100) NOT NULL, 
`Categories` varchar(100) NOT NULL, 
`Website_URL` varchar(150) NOT NULL, 
`Telephone_Number` varchar(20) NOT NULL, 
`Address` varchar(250) NOT NULL, 
`Social_Network_Links` varchar(500) NOT NULL, 
`Short_Description` text NOT NULL, 
`Images` varchar(100) NOT NULL, 
`Full_Description` text NOT NULL, 
`Tags` varchar(500) NOT NULL, 
PRIMARY KEY (`L_ID`) 
) 

,我調用類的這樣的對象:

$listingObj = new Listings($buisnessName, $contactName, $categories, $websiteURL, $telephoneNum,$address, $socialLinks, $shortDescription, $images, $fullDescription,$tags); 
$listingObj->Insert_Listing(); 

我再次」我說我沒有得到任何錯誤,只是代碼不會插入記錄。幫助將不勝感激。謝謝。

回答

0

Business_Name不是Buisness_Name(i/s轉換)相同。修復它在你的WP查詢中。

$wpdb->insert('wp_listings_info', array("Buisness_Name" => $this->buisnessName .. 
//           ^^ 

而且你create聲明:

CREATE TABLE IF NOT EXISTS `wp_listings_info` (
... 
`Business_Name` varchar(100) NOT NULL, 
// ^^ 
... 
) 
+0

謝謝你的更正。雖然我複製粘貼它,但仍然犯了一個錯誤:/ 但是,我仍然繼續收到錯誤但我看到每次運行腳本時都會插入2條相同值的記錄。雖然,我只是調用插入函數一次。奇怪。 – ahmii321

+0

WordPress數據庫錯誤:[] 插入'wp_listings_info'('Business_Name','Contact_Name','Categories','Website_URL','Telephone_Number','Address','Social_Network_Links','Short_Description','Images', '我的商業名稱','我的商業聯繫人名稱','我的分類','我的網站URL','1234567','我的地址','我的社交鏈接','我的短片描述','我的圖片路徑','我的完整描述','我的標籤逗號分隔') – ahmii321

+0

你可以幫助我,讓我知道爲什麼代碼插入2行在我的表中。雖然我只調用構造函數並插入函數一次? – ahmii321