我想插入一些數據到數據表,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