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

标签 php mysql sql wordpress

我试图理解并获取 $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_NameBuisness_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/

相关文章:

sql - 使用不同的 WHERE 条件选择

sql - 获取最近周五日期的SQL

php - PHP 中的 MySQL 日期格式

php - Mysql连接两个表

php - 初学者PHP问题: What the difference between $_POST and $_FILES?

php - 如何使用 2 个 sql 构建关联数组?

SQL 事务 (b) 锁定选择 - 我的理解正确吗

PHPExcel如何在设置样式后取消选择列或范围

mysql - Resque Mysql2::Error: 用户已超出 'max_user_connections' 资源(当前值:10)

MySQL 5.7 未匹配双汉字词