mysql - SELECT selected CHECKBOX and inserted in selected row into DATABASE -


i want input questionnaire answer database

i have table in database(example):

| id | question     | answer | +----+--------------+--------+ | 11 | have dinner? | 1      | | 12 | have house?  | 0      | | 13 | have garden? | null   |  | 14 | bla bla bla? | null   |   // footnote: answer = 0 no, answer = 1 yes 

question how input answer id 13, 14 etc..? database

if controller:

/* 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); // <-- input answer model 

my model:

function inputanswer($answer, $id){          $sql = (" update question     set answer = '$answer' id = '$id' "); $this->db->query($sql); } 

in view: (i still confusing)

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

may want update data in database? try this:

in view:

<?php   echo form_open('controller/method'); foreach($query->result() $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> 

in controller:

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

in model:

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

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -