我试图理解并获取 $wpdb 全局对象,但似乎无法使其工作。基本上,我试图将一条记录插入到 WordPress DB 中的自定义表中。我没有看到 Global $wpdb 对象为我们解决了这个问题,而不是创建自己的连接字符串并使用它。我按照法典示例尝试自己做,但失败了。我没有收到任何错误,但记录也没有插入。花了几个小时来解决这个问题。我们将不胜感激您的帮助。提前致谢。
代码:
<?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();
我再说一次,我根本没有收到任何错误,只是代码也没有插入记录。如有帮助,将不胜感激。谢谢。
最佳答案
Business_Name
与 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,
// ^^
...
)
关于php - $wpdb->insert() 不会在表中插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053992/