1

I am trying to insert data into a custom table. I am using a mysql db with phpmyadmin in my backend. However, I get the following error:

[28-Jun-2018 19:36:05 UTC] WordPress database error  for query INSERT INTO `wp_ticker` (`coin_id`, `price`, `volume_24h`, `market_cap`, `percent_change_1h`, `percent_change_24h`, `percent_change_7d`, `created_at`, `updated_at`) VALUES ('2856', '0.00469682', '1471270', '135309340', '0.56', '-0.45', '-22.2', '2018-06-28 19:36:05', '2018-06-28 19:36:05') made by do_action_ref_array, WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, Market->updateMarket

The structure of the table looks like the following:

CREATE TABLE {$wpdb->prefix}ticker (
    id bigint(20) NOT NULL AUTO_INCREMENT,
    coin_id bigint(20) NOT NULL,
    price longtext NOT NULL,
    volume_24h bigint(20) NOT NULL,
    market_cap bigint(20) NOT NULL,
    percent_change_1h DECIMAL(15,8) NOT NULL,
    percent_change_24h DECIMAL(15,8) NOT NULL,
    percent_change_7d DECIMAL(15,8) NOT NULL,
    created_at datetime NULL,
    updated_at datetime NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (coin_id) REFERENCES {$wpdb->prefix}coins(id)
);  

I am preparing my data like the following:

        $resTicker = array(
            'coin_id' => intval($coin_id),
            'price' => $tick->price,
            'volume_24h' => $tick->volume_24h,
            'market_cap' => floatval($tick->market_cap),
            'percent_change_1h' => floatval($tick->percent_change_1h),
            'percent_change_24h' => floatval($tick->percent_change_24h),
            'percent_change_7d' => floatval($tick->percent_change_7d),
        );

        //check if the above ticker record exists already in the db
        $recordTickerExists = $wpdb->get_results(
        $wpdb->prepare(
             "SELECT * FROM {$wpdb->prefix}ticker
             WHERE 
                 price = %s
                 AND coin_id = %s 
                 AND market_cap = %s 
             LIMIT 1",
             floatval($tick->price), $coin_id, floatval($tick->market_cap)
            )
        );

        if ( $recordTickerExists == 0 || $recordTickerExists == null ) {
            // ticker does not exists else do nothing
            try {
                $resTicker['created_at'] = date('Y-m-d H:i:s');
                $resTicker['updated_at'] = date('Y-m-d H:i:s');
                $wpdb->insert("{$wpdb->prefix}ticker", $resTicker);
            } catch (\Exception $ex) {
              // ...  
            }
        }

Any suggestions what I am doing wrong or see further information on the error?

I appreciate your replies!

3
  • 1
    Why do you use coin_id and market_cap as string in your select query? Commented Jun 28, 2018 at 20:43
  • 1
    have you tried running generated query in PHPMyAdmin? wpdb doesn't throw exceptions, it has its own error handling mechanism Commented Jun 29, 2018 at 1:16
  • @TomJNowell Yes, I ran the insert query in phpmyadmin and the weird thing is that it does not throw any exception back there... Commented Jun 29, 2018 at 4:39

1 Answer 1

1

You're inserting a float into market_cap, which is of bigint type. Doesn't seem to be the thing that would cause the issue in this case, but you might want to use intval() instead of floatval() or change the field to one that'll handle a float.

1
  • Thank you for your answer! However, I still get the above error. Any suggestions how to find out further information? Commented Jun 30, 2018 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.