【发布时间】: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