我在从数据库中插入单个数据时遇到问题
示例:我有两个数据,即 ID:1 的问题 1 和 ID:2 的问题 2:。这两个问题确实有不同的按钮。问题是,一旦我单击 question1 或 question2 按钮,它就会在我的数据库中插入两个 ID。
这是我的控制器
$data['posts'] = $this->Post_Model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
$this->load->library('form_validation');
if($this->form_validation->run()==FALSE) {
if($this->input->post("add")) {
$this->Post_Model->count_up();
redirect('posts');
}
}
我的模特
function count_up(){
for($i=0; $i<count($this->input->post('hidden')); $i++){
$data = array(
'post_id' => $this->input->post("hidden[$i]")
);
$this->db->insert("userspost", $data);
}
}
我的观点
<?php
$iq = 0;
$i = 0;
$arrtry = array();
foreach($posts->result() as $post){
?>
<br>
<div class="card card-nav-tabs">
<div class="card-header card-header-primary">
<!-- colors: "header-primary", "header-info", "header-success", "header-warning", "header-danger" -->
<div class="nav-tabs-navigation">
<input class="form-control" value="<?php echo $arrtry[$iq++] = $post->id; ?>" name="hidden1[<?php $i; ?>]" type="hidden">
<input class="btn btn-primary" type="submit" name="add" value="UP" />
</div>
</div>
</div>
<?php
$i++;
}
?>
我的问题是我想插入 question2 ID 而不插入 question1 ID。希望大家帮忙谢谢!
