当用户在投票时选择它时,我试图增加数据库中一个选项的值。我没有收到任何错误,但数据库没有更新。我究竟做错了什么?
在我的控制器中:
public function vote($option_id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('options', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->helper('html');
$data['content'] = $this->load->view('user/vote','', TRUE);
$this->load->view('templates/master', $data);
}
else{
$checkedoption = intval($_POST['options']);
$optionid = $this->polls_model->get_option($option_id);
$this->polls_model->set_votes($checkedoption, $option_id);
$this->load->helper('html');
$data['content'] = $this->load->view('user/success','', TRUE);
$this->load->view('templates/master', $data);
}
}
在我的模型中,我有这样的功能:
//this gets the id of the option to vote on
function get_option($option_id)
{
$query = $this->db->get_where('pollOption', array('option_id'=>$option_id));
return $query->row_array();
}
// THis updates the vount_count column in the database:
public function set_votes($checkedoption, $option_id)
{
$this->db->where('option_id', $checkedoption);
$this->db->set('vote_count', 'vote_count+1', FALSE);
$this->db->update('pollOption');
}
在我看来,我有这个:
$poll_id = $polls_item['id'];
$query = $this->db->get_where('pollOption', array('poll_id' => $poll_id));
$queryResult = $query->result_array();?>
<html>
<head>
<script type="text/javascript" src="<?php echo base_url();?>js/jquery.js"></script>
</head>
<body>
<?php echo form_open('user/vote/' .$poll_id);
foreach ($queryResult as $row):?>
<input type="radio" name="options" value = "<?php echo $row['option_item'];?>" > <?php echo $row['option_item'];?> <br>
<?php endforeach;?>
<input type="submit" name="submit_vote" value="Vote">
<?php echo form_close(); ?>
谢谢你。