$ wpdb-> insert()不会在表中插入记录

时间:2013-09-27 15:00:57

标签: php mysql sql wordpress wpdb

我正在尝试理解并掌握$ wpdb全局对象,但似乎无法使其工作。基本上,我正在尝试将单个记录插入到wordpress DB中的自定义表中。我没有创建自己的连接字符串并使用它,而是看到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();

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

1 个答案:

答案 0 :(得分: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,
// ^^
...
)