0

我试图理解并掌握 $wpdb 全局对象,但似乎无法使其工作。基本上,我试图将一条记录插入到 wordpress 数据库中的自定义表中。我没有创建自己的连接字符串并使用它,而是看到 Global $wpdb 对象为我们解决了问题。我按照 codex example 并尝试自己做,但失败了。我没有收到任何错误,但记录也没有插入。解决这个问题需要几个小时。您的帮助将不胜感激。提前致谢。

代码:

<?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();

我再次说我根本没有收到任何错误,只是代码也没有插入记录。帮助将不胜感激。谢谢。

4

1 回答 1

0

Business_Name与( i/s 切换)不同。Buisness_Name在您的 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,
// ^^
...
)
于 2013-09-27T15:23:57.507 回答