我想插入一些数据到数据表,wordpress插件。 数据是通过POST获取的。 我有多个结果(采取后),但$ wpdb->插入只插入我最后的结果(实际上覆盖数据)。这是为什么?Wordpress插入多行到数据表
这里是我的代码:
HTML:
echo '<label for="reduceri-post-category"><b>' . __("What categories should be the adverts", 'appplugin') . '</b></label><br /><br /><br />';
?>
<?php foreach ($the_data[categories] as $sat): ?>
<b> <?= $sat[name]; ?> <br /> </b>
<?php foreach ($sat[subcategories] as $cat):
?>
<input type="checkbox" name="reduceri-post-category" value="<?= $cat[sid] ?>" /> <?php echo $cat[name]; echo $cat[sid]; ?><br />
<? endforeach; ?>
<? endforeach; ?>
global $wpdb;
$thedata['reduceri-post-category'] = $_POST['reduceri-post-category'];
$table_name = $wpdb->prefix . "reduceri";
foreach ($thedata as $key => $value) {
if($post->post_type == 'revision') return;
if ($post->post_type == 'post'){
$wpdb->insert($table_name, array('time' => current_time('mysql'), 'post' => $post->ID, 'category' => $value));
}
}
我该怎么才能做的是能够插入所有的结果,不仅是最后一个?非常感谢!
从快速浏览代码我会说它只能插入一次,而不是多次。而你的代码可以从正确的缩进中受益。 – hakre