【问题标题】:$wpdb->insert produces " Duplicate entry '0-0' for Key '1' "$wpdb->insert 为键 '1' 生成“重复条目 '0-0'”
【发布时间】:2012-02-24 11:26:38
【问题描述】:

我正在编写一个插件并尝试在 foreach 循环内的 wp_term_relationships 表中插入一个新行。由于 var_dump,我知道变量具有值,但由于某种原因,我一直收到错误。这在 show_errors() 函数中出现了大约 600 次:

WordPress 数据库错误:[键 1 的重复条目 '0-0'] 插入 进入wp_term_relationships (object_id,term_taxonomy_id,term_order) 值 ('','','')

我的代码:

foreach ($cb_t2c_cat_check as $values) {
        global $wpdb;
        $prefix = $wpdb->prefix;

        $table = $prefix . 'term_relationships';
        $object_id = $values->object_id;
        $taxo_id = $values->term_taxonomy_id;
        $num_object_id = (int)$object_id;
        $num_taxo_id = (int)$taxo_id;

        //var_dump($num_object_id); //This produces values, so why are they not getting inserted into the table?
        //var_dump($num_taxo_id); //This produces values, so why are they not getting inserted into the table?

        $wpdb->insert( 
            $table, 
            array( 
                'object_id' => $num_object_id, 
                'term_taxonomy_id' => $num_taxo_id,
                'term_order' => 0
                ), '' 
            ); 

        //$wpdb->show_errors();
        //$wpdb->print_error();
        }

【问题讨论】:

  • 作为更新,我尝试使用 '$wpdb->query("INSERT INTO...' 代替,但这会导致类似的错误(尽管至少这些值反映在错误中: WordPress database error: [Duplicate entry '9-61' for key 1] INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) VALUES ('9', '61', '0')

标签: php mysql wordpress insert-into


【解决方案1】:

至于为什么不起作用:不要将$wpdb->insert的第三个参数设置为空字符串。它相应地格式化每个字段..

它现在所做的相当于:

$wpdb->insert($table, array(
            'object_id' => sprintf('', $num_object_id), 
            'term_taxonomy_id' => sprintf('', $num_taxo_id),
            'term_order' => sprintf('', 0)
));

如果你真的想设置第三个参数,你应该这样做:

$wpdb->insert($table, array(
            'object_id' => $num_object_id, 
            'term_taxonomy_id' => $num_taxo_id,
            'term_order' => 0
), array('%d', '%d', '%d'));

至于错误:wp_term_relationships 表在 (object_id, term_taxonomy_id) 上有一个唯一的主键。这意味着您不能在该表中有两行同时具有相同的 object_id 和 term_taxonomy_id。

虽然发生这种情况是因为通过将 insert 的第三个参数设置为空字符串,您试图一遍又一遍地插入 object_id=0 和 term_taxonomy_id=0 的行。

【讨论】:

  • 因此,如果我阅读正确,我的 SQL 查询中有错误,因此我试图插入已经是唯一主键的行。我需要 1) 修改查询以过滤掉任何已经是 object_id/term_taxonomy_id 对的行,然后我需要 2) 将每个参数的第三个参数设置为 '%d'?这对吗?非常感谢您的帮助。
  • 先试试 2)。您可以将第三个参数设置为 array('%d', '%d', '%d') 或将其全部省略。但不要将其设置为空字符串。
【解决方案2】:

上面的答案是正确的,数据库需要有唯一的键,不能插入已经存在键值对的行,并且需要设置每个新值​​的格式。此外,对于 Wordpress,还有一个我没有解决的问题,特别是处理 term_taxonomy 表和更新计数。

首先要注意,该插件旨在更新 term_relationships 表中帖子的某些类别。这实际上是使用 $wpdb-> insert 方法完成的。但是,我确定插件是否真的在 term_relationships 表中插入新行的测试不是直接看表,而是去 Wordpress 仪表板,选择类别,看看该类别的帖子数量是否超过前。这不起作用,因为插件从未更新 term_taxonomy 表中的计数。我只是通过单击 Wordpress 仪表板中某个类别旁边的“查看”并看到该类别的多个帖子才发现这一点,尽管官方的 Wordpress“计数”说没有。

我确认 term_taxonomy 表,即 'count' 列,也需要通过直接进入数据库并将 WHERE = 'term_taxonomy_id' 放入语句中进行更新。果然,有超过 1700 个结果,尽管 Wordpress 认为没有。

课程:使用 PHPMyAdmin 确认 $wpdb->insert 方法是否有效,不一定要依赖 Wordpress 仪表板。

经过一些修改,代码现在运行良好。这是一个例子:

foreach ($cb_t2c_objects as $values) {
        global $wpdb;
        $prefix = $wpdb->prefix;

        $table = $prefix . 'term_relationships';
        $object_id = $values->object_id;
        $taxo_id = $values->cat_taxo;
        $num_object_id = (int)$object_id;
        $num_taxo_id = (int)$taxo_id;

        //Need to check to see if row exists for each, if not, then insert.
        $cb_t2c_get_row = $wpdb->get_row("
            SELECT * 
            FROM ".$prefix."term_relationships
            WHERE object_id = ".$num_object_id." AND term_taxonomy_id = ".$num_taxo_id."
            GROUP BY object_id, term_taxonomy_id
        ", OBJECT);

        //var_dump($cb_t2c_get_row);

        if ( is_null($cb_t2c_get_row) ) {
            //Insert the new values.
            $wpdb->insert( 
            $table, 
            array( 
                'object_id' => $num_object_id, 
                'term_taxonomy_id' => $num_taxo_id,
                'term_order' => 0
                ), 
            array(
                '%d', 
                '%d', 
                '%d'
                ) 
            );
        }

        //Set the variables for the count update.
        $cb_t2c_term_taxonomy_table = $prefix . 'term_taxonomy';
        $cb_t2c_update_data = $wpdb->get_var("
            SELECT count(term_taxonomy_id) as 'new_count'
            FROM ".$prefix."term_relationships
            WHERE term_taxonomy_id = ".$num_taxo_id."
        ",0,0); //returning NULL

        //var_dump($cb_t2c_update_data);

        //Update the count in the term_taxonomy table.
        $wpdb->query("
            UPDATE ".$prefix."term_taxonomy
            SET count = ".$cb_t2c_update_data."
            WHERE term_taxonomy_id = ".$num_taxo_id."
        ");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-03
    • 2017-08-07
    • 2018-02-20
    • 2014-09-04
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多