【问题标题】:$wpdb->insert() does not Insert record in a table$wpdb->insert() 不在表中插入记录
【发布时间】:2024-05-11 01:35:01
【问题描述】:

我正在尝试理解并掌握 $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();

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

【问题讨论】:

    标签: php mysql sql wordpress


    【解决方案1】:

    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,
    // ^^
    ...
    )
    

    【讨论】:

    • 感谢指正。虽然我复制粘贴它但仍然犯了一个错误:/ 但是,我仍然不断收到错误但是我看到每次运行脚本时都会插入 2 条相同值的记录。虽然,我只是调用了一次插入函数。奇怪。
    • WordPress 数据库错误:[] INSERT INTO wp_listings_info (Business_Name,Contact_Name,Categories,Website_URL,Telephone_Number,Address,Social_Network_Links,@987 @,Images,Full_Description,Tags) VALUES ('我的业务名称','我的业务联系人姓名','我的类别','我的网站 URL','1234567','我的地址','我的社交链接'、'我的简短描述'、'我的图片路径'、'我的完整描述'、'我的标签逗号分隔')
    • 你能不能帮帮我,让我知道为什么代码在我的表中插入 2 行。虽然我只调用了一次构造函数和插入函数??
    • 事实是你不是。我会告诉你尝试在构造函数/插入函数中运行print_r(debug_backtrace()),看看你是否在其他地方创建了这个类。