我想将问卷 回答输入数据库
我在数据库(示例)中有这样的表:
| 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"/>
答案 0 :(得分:1)
您可能想要更新数据库中的数据吗?试试这个:
在视图中:
<?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);
}
}
}
}
?>
答案 1 :(得分: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"/>