0

This code works correctly down to the eighth line from the bottom. The SELECT statement there does not find the one record that was just inserted. It is the only record in the table at this point. What am I doing wrong??

global $wpdb;

//Get post id for auction from post where auction is inserted. Used to track bids db.
$postid = get_the_id();
$starting_bid = 25;
$starting_email = "myemail@gmail.com";

//Creates jwp_bids table in database if it doesn't exist.
$table = $wpdb->prefix . "jwp_bids"; 
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(15) NOT NULL AUTO_INCREMENT,
    `bid_amt` decimal(10,2) NOT NULL,
    `email` varchar(255) NOT NULL,
    `bids_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,  
    `post_id` mediumint(15) NOT NULL,
     PRIMARY KEY (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

    $count = $wpdb->get_var("SELECT COUNT(*)
        FROM $table WHERE post_id IS NOT NULL");

        if($count == 0){
            $insert_db = $wpdb->insert( 
                    $table, 
                        array(
                            'email' => $starting_email,
                            'bid_amt' => $starting_bid,
                            'post_id' => $postid,
                            )
                );
        }
    // We now have one record in the table representing the starting bid.       
    //Gets highest bid information and assigns to variables for display in post.
    $highest_bid_info = $wpdb->get_results(
                        "SELECT max(bid_amt) AS bid, bid_time, email
                        FROM $table
                        WHERE post_id = $postid", ARRAY_A );
    $highest_bid_info = array_shift ( $highest_bid_info );
    $high_bid = $highest_bid_info['bid'];
    $high_bidder = $highest_bid_info['email'];
    $bid_time = $highest_bid_info['bid_time'];
0

look for $wpdb->last_error. It may help you to be helped yourself.

  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – cjbj Jan 17 '17 at 6:14
  • 1
    Thanks for that :) Error revealed "bids_time" which should have been "bid_time" when creating table at the beginning. ANNOYING extra "s"! :) – James Jan 17 '17 at 6:17
  • 1
    Now I have learned a new tool :) – James Jan 17 '17 at 6:18
  • @cjbj I do not know if you are aware of wordpress or not but I find that the function was his need and the function is self explaining.. Thanks. – Vishal Kumar Sahu Jan 17 '17 at 8:01
  • @James In wordpress one word ruins and makes your mood... :p – Vishal Kumar Sahu Jan 17 '17 at 8:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.