查询INSERT到时出现WordPress数据库错误

时间:2018-06-28 作者:Carol.Kar

我正在尝试将数据插入到自定义表中。我在后端使用带有phpmyadmin的mysql数据库。但是,我得到以下错误:

[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
表的结构如下所示:

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)
);  
我正在准备以下数据:

        $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) {
              // ...  
            }
        }
有什么建议我做错了什么,或者看到关于错误的更多信息吗?

感谢您的回复!

1 个回复
SO网友:Chris Cox

您正在将浮点插入market_cap, 其中bigint 类型在这种情况下,似乎不是导致问题的原因,但您可能希望使用intval() 而不是floatval() 或者将字段更改为处理浮点的字段。

结束

相关推荐

使用MySQL将一个自定义字段值替换为另一个自定义字段值

我有两个自定义字段,都有数值:tnid\\u 01tnid\\U 01旧所有帖子都存在自定义字段“tnid\\u 01”。自定义字段“tnid\\u 01old”仅适用于某些帖子。我正在尝试将“tnid\\u 01”的值替换为“tnid\\u 01old”,但前提是“tnid\\u 01old”存在。这就是我目前所知道的,但我得到了一个mysql错误:Update wp_postmeta (post_id, meta_key, meta_value)( SELECT post_id, \'tnid