2013-08-16 53 views
0

我想输入我的问卷答案到数据库选择选定复选框,并插入选中的行导入数据库

我有台这样的数据库(例如):

| id | question  | answer | 
+----+--------------+--------+ 
| 11 | have dinner? | 1  | 
| 12 | have house? | 0  | 
| 13 | have garden? | NULL | 
| 14 | bla bla bla? | NULL | 

// footnote: answer = 0 as no, answer = 1 as yes 

问题如何将我的回答输入id 13,14等。到数据库

如果我的控制器:

/* I am using CodeIgniter */ 

$data['form_action'] = site_url('dcm/index'); 
$answer = $this->input->post('answer'); 
$id = $this->input->post('id'); 
$selected_answer = $answer['id']; 
$this->dcm_model->inputAnswer($answer, $id); // <-- for input answer to model 

我的模型:

function inputAnswer($answer, $id){   
$sql = (" 
UPDATE question 
    SET answer = '$answer' 
WHERE id = '$id' 
"); $this->db->query($sql); 
} 

鉴于:(我仍然扑朔迷离

<?php foreach($query->result() as $row) { ?> 
<span> <?php echo $row->id . '. ' . $row->question; ?> </span> 
<span> <input type="checkbox" value="1" name="answer" /> </span> 
<input type="submit" value="submit answer"/> 

回答

1

可能是你想要更新你的数据数据库?试试这个:

在View:

<?php 
echo form_open('controller/method'); 
foreach($query->result() as $row) { ?> 
<span> <?php echo $row->id . '. ' . $row->question; ?> </span> 
<span> <input type="checkbox" value="<?=$row->id?>" name="answer[]" /> </span> 
<?php } ?> 
<input type="submit" value="submit answer"/> 
</form> 

在控制器:

<?php 
    $this->dcm_model->inputAnswer(); 
?> 

在型号:

<?php 
    function inputAnswer(){ 
     $data = array(); 
     if($this->input->post('answer')){ 
      $ans = $this->input->post('answer'); 
      foreach($ans as $each){ 
       if(isset($each) && $each != ""){ 
        $data['answer'] = '1'; 
        $this->db->where('id', $each)->update('questionnaire', $data); 
       } 
      } 
     } 
    } 
?> 
+0

我用这个,但它不会改变我的数据库表什么...这是在控制器吗? “$这 - > dcm_model-> inputAnswer();” ?? –

0

使用MVC图案像这样和使用update_batch

在控制器得到表的详细信息

public function my_function(){  

     $id_array = array(); 

     $results = $this->my_model->get_results(); 
     foreach($results as $result) 
     { 
      array_push($id_array,$result['id']); 
     } 

     if($this->input->post()) 
     { 
      $checked_id_array = array(); 
      $checked_ans_Arr = $this->input->post('answer'); 

      $update_array = array(); 

      foreach($checked_ans_Arr as $checked_ans) 
      { 
       $update_array['id'] = $checked_ans; 
       $update_array['answer'] = 1; 
       array_push($checked_id_array,$checked_ans); 
      } 

      $not_checked_ids = array_diff($checked_id_array,$id_array); 

      if(is_array($not_checked_ids) && count($not_checked_ids)>0) 
      { 
       foreach($not_checked_ids as $not_checked_id) 
       { 
        $update_array['id'] = $not_checked_id; 
        $update_array['answer'] = 0; 
       } 
      } 

      $this->my_model->update_batch_answers($update_array); 

      $results = $this->my_model->get_results(); 
     } 

     $this->load_view('your_view',array('results'=>$results));  

    } 

模型

public function get_results() 
{ 
    $this->db->select('*'); 
    $his->db->from('table'); 

    $query = $this->db->get(); 
    return $query->result_array(); 
} 

public function update_batch_answers($update_array) 
{ 
    return $this->db->update_batch('mytable', $update_array, 'id'); 
} 

鉴于

<?php foreach($results as $result) { ?> 

<span><?php echo $result['question']; ?></span> 
<span> <input <?php if($result['answer']){echo "checked='checked'";} ?> type="checkbox" value="<?php echo $result['id']; ?>" name="answer[]" /> </span> 

<?php } ?> 

<input type="submit" value="submit answer"/>