1

I'm trying to understand and get hold of the $wpdb global object but can't seem to make it work. Basically, I'm trying to insert a single record into a custom table in wordpress DB. Instead of creating your own connection string and using that, I've seen the Global $wpdb object do the trick for us. I followed the codex example and tried to do it on my own but failed. I don't get any error but the record does not insert as well. It been hours figuring the issue. Your help would be appreciated. Thanks in advance.

Code:

<?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"));
    }

}
?>

Table Structure:

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`)
)

and I'm calling the object of the class like this:

$listingObj = new Listings($buisnessName, $contactName, $categories, $websiteURL, $telephoneNum,$address, $socialLinks, $shortDescription, $images, $fullDescription,$tags);
$listingObj->Insert_Listing();

Again I'm saying that I'm not getting any error at all, It's just the code does not insert the record as well.

EDIT:

Ok, After debugging I get this error but 2 records having the same value are inserted each time the script runs. Though Insert function is only being called once.

I get this error:

WordPress database error: [Unknown column 'Buisness_Name' in 'field list']
INSERT INTO wp_listings_info ( 
    Buisness_Name, Contact_Name, Categories, Website_URL,
    Telephone_Number, A‌ddress, Social_Network_Links, Short_Description, 
    Images, Full_Description, T‌​ags
) VALUES (
    'My Buisness Name', 'My Buisness Contact Name', 'My Categories',
    'My Website URL','1234567','My Address','My Social Links',
    'My Short Descriptions','My images Path','My Full Description',
    'My Tags comma seperated'
)
  • 1
    debugging information? – s_ha_dum Sep 27 '13 at 15:05
  • Thanks ! I get this error: WordPress database error: [Unknown column 'Buisness_Name' in 'field list'] INSERT INTO wp_listings_info (Buisness_Name,Contact_Name,Categories,Website_URL,Telephone_Number,Address,Social_Network_Links,Short_Description,Images,Full_Description,Tags) VALUES ('My Buisness Name','My Buisness Contact Name','My Categories','My Website URL','1234567','My Address','My Social Links','My Short Descriptions','My images Path','My Full Description','My Tags comma seperated') – ahmii321 Sep 27 '13 at 16:25
  • What error? Edit the information into the question. Do Not post it into a comment. – s_ha_dum Sep 27 '13 at 16:28
  • Done. But I get another issue. It inserts 2 records of the same value each time I run the script showing the above error. – ahmii321 Sep 27 '13 at 16:48
  • Did you notice the typo on the column name? Buisness_Name instead of Business_Name – Ricardo Nuñez Sep 27 '13 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.