Php $wpdb->;插入“生产”;重复输入';0-0';对于键';1'&引用;
我正在编写一个插件,并试图在foreach循环的wp_term_relationships表中插入一个新行。我知道变量有值是因为var_转储,但由于某些原因,我一直得到一个错误。这在show_errors()函数中显示了大约600次: WordPress数据库错误:[键1的重复条目“0-0”]插入 进入Php $wpdb->;插入“生产”;重复输入';0-0';对于键';1'&引用;,php,mysql,wordpress,insert-into,Php,Mysql,Wordpress,Insert Into,我正在编写一个插件,并试图在foreach循环的wp_term_relationships表中插入一个新行。我知道变量有值是因为var_转储,但由于某些原因,我一直得到一个错误。这在show_errors()函数中显示了大约600次: WordPress数据库错误:[键1的重复条目“0-0”]插入 进入wp\u术语\u关系 (object\u id,term\u taxonomy\u id,term\u order)值(“”,“”,“”) 我的代码: foreach ($cb_t2c_cat_c
wp\u术语\u关系
(object\u id
,term\u taxonomy\u id
,term\u 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->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\u term\u relationships表上有一个唯一的主键(object\u id,term\u taxonomy\u id)。这意味着该表中不能有两行同时具有相同的object\u id和term\u taxonomy\u id
尽管发生这种情况是因为通过将insert的第三个参数设置为空字符串,您正在尝试一次又一次地插入object_id=0和term_taxonomy_id=0的行。上述答案是正确的,因为数据库需要具有唯一的键,并且无法插入已经存在键值对的行,需要设置每个新值的格式。另外,对于Wordpress,有一个我没有解决的问题,特别是处理term_分类表和更新计数 首先需要注意的是,该插件旨在更新term_relationships表中帖子的特定类别。这实际上是使用$wpdb->insert方法完成的。然而,我判断插件是否真的在term_relationships表中插入了新行的测试不是直接查看表,而是转到Wordpress仪表板,选择类别,然后查看该类别的帖子数量是否比以前多。这不起作用,因为插件从未更新term_分类表中的计数。我只是通过点击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."
");
作为更新,我尝试使用“$wpdb->query”(“INSERT-INTO…”),但这会导致类似的错误(尽管至少这些值反映在错误中:
WordPress数据库错误:[键1的重复条目“9-61”)插入wp\u-term\u关系(对象标识、术语分类标识、术语顺序)值('9',61',0'))
因此,如果我阅读正确,我的SQL查询中会出现错误,因此我试图插入已经是唯一主键的行。我需要1)修改查询以筛选出任何已经是对象\u id/术语\u分类\u id对的行,然后我需要2)将第三个参数分别设置为“%d”是否正确?非常感谢您的帮助。请先尝试2)。您可以将第三个参数设置为数组(“%d”、“d”、“d”)
或将其全部忽略。但不要将其设置为空字符串。