I am trying to insert a row into a table.

Given below are the various functions to create table. insert data into the table- but its not working. Also given below is the sample output showing all fields that are being attempted to be inserted.

/* Function: Insert Zillow property data into table
* Input: $zillowid String, $date DateTime
* Output: Boolean
function zillowInsertData($zillowid, $date_val, $postid, $url) {
global $wpdb;
$table_name = $wpdb->base_prefix."Zillow";
if(trim($date_val) == '')
    $date_val= string2DateTime("0000-00-00");
$sql = "
    INSERT INTO $table_name (ZillowID, PostID, PropertyURL, DateUpdated)
    VALUES ($zillowid, $postid, $url,$date_val)";
return $wpdb->query($sql)===1;

echo " Values being inserted- 
    Zp id=" . $current_zpid . ", 
    postid=" . $post_id, "url=" . $propertyurl;
zillowInsertData($current_zpid, "", $post_id, $propertyurl );

Sample output showing values being inserted into the table-

About to insert row in Zillow table 
Values being inserted- 
    Zp id=51605333, 

Table creation code--

global $wpdb;
    $table_name = $wpdb->base_prefix."Zillow";
    $sql = "CREATE TABLE $table_name(
        ZillowID text NOT NULL, PostID text NOT NULL,
        PropertyURL text, 
        DateUpdated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL

What am I doing wrong here?

Update-- The string2datetime function uses strtotime php function.

Thanks, Arvind.

  • 2
    can you provide the error message(s) that appear? – Jen Jun 18 '13 at 15:57
  • 1) What does string2DateTime() return? Is there a compelling reason to not use native PHP functions like date( 'Y-m-d H:i:s', 0 );? 2) $wpdb->base_prefix is useful in Multisite if you want to tell $wpdb to use the root site's tables. If you want to add to the current site's tables -- or if you're not using Multisite -- you should use $wpdb->prefix instead. – Pat J Jun 18 '13 at 16:00
  • @PatJ I have updated the post. Also, I can use $wpdb->prefix, but in other functions also I am using base_prefix and its working(I understand your point- but that is not the problem here) – Arvind Jun 18 '13 at 16:05
  • @guiniveretoo could you let me know how to output the error message? Do you want me to directly run the query in DB(after echoing it from the function just before the function runs that query)? Or would you suggest some way of getting the error from $wpdb->query? How would I do that? THanks – Arvind Jun 18 '13 at 16:07
  • 1
    wpdb: Show and Hide SQL errors – Milo Jun 18 '13 at 16:09

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.