updating multiple values to mysql database using codeigniter
my post data from form is coming as
Array
(
[radiogroup_1] => 1
[radiogroup_2] => 4
[radiogroup_4] => 11
[submit] => submit
)
and my model is
function add_votes(){
foreach($POST[] as $k=> $v){
$id = $v;
}
$this->db->where('answerid','radiogroup_'. $id );
$this-&g开发者_如何学Got;db->set('votes', 'votes+1',FALSE);
$this->db->update('vote_table');
}
apparently, it's not working.
Maybe
function add_votes(){
foreach($_POST[] as $k=> $v){
$this->db->where('answerid','radiogroup_'. $v );
$this->db->set('votes', 'votes+1',FALSE);
$this->db->update('vote_table');
}
}
or
function add_votes(){
foreach($_POST[] as $k=> $v){
$this->db->where('answerid', $v );
$this->db->set('votes', 'votes+1',FALSE);
$this->db->update('vote_table');
}
}
Dont need to foreach to update. Just use update the table one time.
function add_votes(){
$radiogroup_1=$this->input->post('radiogroup_1');
$radiogroup_2=$this->input->post('radiogroup_2');
$radiogroup_4=$this->input->post('radiogroup_4');
$data=array('radiogroup_1'=>$radiogroup_1,'radiogroup_2'=>$radiogroup_2,'radiogroup_4'=>$radiogroup_4);
$this->db->where('answerid','radiogroup_'. $id );
$result=$this->db->update('vote_table',$data);
}
精彩评论