增加数据库codeigniter中的值

时间:2012-10-06 03:13:27

标签: database codeigniter radio-button

当我在投票时选择它时,我试图增加数据库中某个选项的值。我没有收到任何错误,但数据库没有更新。我做错了什么?

在我的控制器中:

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(); ?>

三江源。

2 个答案:

答案 0 :(得分:0)

您是否尝试过手动运行查询?

然后,您是否尝试执行echo $this->db->last_query();来比较手动查询和CodeIgniter正在做什么?

或者,您可以选择值,将其分配给变量,使用PHP递增,然后执行$this->db->set('vote_count', $incremented_value);

答案 1 :(得分:0)

尝试更改

  $this->db->set('vote_count', 'vote_count+1', FALSE);

  $this->db->set('vote_count = vote_count + 1');