$wpdb->插入产生”重复条目“0-0”对于键“1”; ”
我正在编写一个插件,并尝试在 foreach 循环内的 wp_term_relationships 表中插入一个新行。我知道变量由于 var_dump 而具有值,但由于某种原因,我始终收到错误。这在 show_errors() 函数中出现了大约 600 次:
WordPress 数据库错误:[键 1 的重复条目“0-0”] INSERT INTO
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();
}
I'm writing a plugin and trying to insert a new row into the wp_term_relationships table inside of a foreach loop. I know the variables have values because of a var_dump, but for some reason, I'm getting an error consistently. This shows up about 600 times on the show_errors() function:
WordPress database error: [Duplicate entry '0-0' for key 1] INSERT
INTOwp_term_relationships
(object_id
,term_taxonomy_id
,term_order
) VALUES ('','','')
My Code:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于为什么不起作用:不要将
$wpdb->insert
的第三个参数设置为空字符串。它相应地格式化每个字段。它现在所做的相当于:
如果您确实想设置第三个参数,您应该这样做:
至于错误:wp_term_relationships 表在 (object_id, term_taxonomy_id) 上有一个唯一的主键。这意味着该表中不能有两行同时具有相同的 object_id 和 term_taxonomy_id。
尽管发生这种情况是因为通过将 insert 的第三个参数设置为空字符串,您试图一遍又一遍地插入 object_id=0 和 term_taxonomy_id=0 的行。
As for why it does not work: do not set third parameter of
$wpdb->insert
to empty string. It formats every field accordingly..What it does now is equivalent to:
If you really want to set third parameter you should do:
As for error: wp_term_relationships table has a unique primary key on (object_id, term_taxonomy_id). This means that you cannot have two rows in that table which have both same object_id and term_taxonomy_id.
Though this has happened because by setting third parameter of insert to empty string, you are trying to insert rows with object_id=0 and term_taxonomy_id=0 over and over again.
上面的答案是正确的,因为数据库需要有唯一的键,并且不能插入键值对已经存在的行,并且需要设置每个新值的格式。此外,针对 Wordpress,有一个我没有解决的问题,特别是处理 term_taxonomy 表和更新计数。
首先,需要注意的是,该插件旨在更新 term_relationships 表中帖子的某些类别。这实际上是使用 $wpdb-> 完成的插入方法。然而,我确定插件是否实际在 term_relationships 表中插入新行的测试并不是直接查看该表,而是转到 WordPress 仪表板,选择类别,然后查看该类别的帖子数量是否超过前。这不起作用,因为插件从未更新 term_taxonomy 表中的计数。我只是通过单击 WordPress 仪表板中某个类别旁边的“查看”并看到有多个属于该类别的帖子才发现这一点,尽管官方 WordPress“计数”说没有。
我确认 term_taxonomy 表(“count”列)也需要更新,方法是直接进入数据库并将 WHERE = 'term_taxonomy_id' 放入语句中。果然,有超过 1700 个结果,尽管 Wordpress 认为没有。
课程:使用 PHPMyAdmin 确认 $wpdb->insert 方法正在运行,而不一定依赖于 Wordpress 仪表板。
经过一些修改,代码现在运行良好。这是一个例子:
The answer above was correct in that the database needs to have unique keys and cannot insert a row where the key-value pair already exists, and the format of each new value needs to be set. In addition, specific to Wordpress, there was a problem I wasn't addressing, specifically dealing with the term_taxonomy table and updating the count.
First it's important to note that the plugin was designed to update certain categories for posts in the term_relationships table. This was actually accomplished using the $wpdb-> insert method. However, my test for determining whether the plugin actually inserted new rows in the term_relationships table was not to look at the table directly, but to go to the Wordpress dashboard, select categories, and see if the number of posts with that category was more than before. This didn't work, because the plugin never updated the count in the term_taxonomy table. I only discovered this by clicking 'view' next to a category in the Wordpress dashboard and seeing that there were multiple posts with that category, even though the official Wordpress "count" said there were none.
I confirmed that the term_taxonomy table, the 'count' column, needed to be updated as well by going straight to the database and putting WHERE = 'term_taxonomy_id' in the statement. Sure enough, there were over 1700 results, even though Wordpress thought there were none.
Lesson: Confirm the $wpdb->insert method is working by using PHPMyAdmin, not necessarily relying on the Wordpress dashboard.
With a few modifications, the code now works great. Here's an example: