单击按钮时,在Codeigniter中更新数据库

时间:2016-10-13 18:41:08

标签: php codeigniter

单击按钮时使用Codeigniter我需要更新来自' PENDING'的studentstatus字段。到' ACTIVE'。但它不起作用。 表:studentstatus。 领域:acceptid,studentid,studentstatus

控制器:

function updateStat (){
    $this->load->model("mdl_studentstatus");
    if ($this->input->post('mysubmit')) {
      $this->mdl_studentstatus->upddata($this->input->post());
   }
}

模型:

public function upddata() {
     $this->db->set('studentstatus', 'ACCEPT', FALSE);
     $this->db->where('studentstatus', 'PENDING');
     $this->db->update('studentstatus');
     return true;
  }

查看文件:

<table>
     <tr>
      <td><strong>Student Id</strong></td>
      <td><strong>Student Status</strong></td>
    </tr> 
     <?php foreach($status as $stat){?>
     <tr>
         <td><?php echo $stat->studentid;?></td>
         <td><?php echo $stat->studentstatus;?></td>
           <td> <fieldset class="submit_field">
              <?php echo form_submit('mysubmit', 'ACCEPT'); ?>
           </fieldset></td>

      </tr>     
     <?php }?>  
   </table>

1 个答案:

答案 0 :(得分:0)

控制器中的

$words = explode(' ', $string);
$previous = null;
foreach ($words as $key => $value) {
    if ($value == $previous) unset($words[$key]);
    $previous = $value;
}
$string = implode(' ', $words);

在您的模型中

public function update()
{
    $this->load->model("mdl_studentstatus");
    if ($this->input->post('submit')) {
      $query     = $this->mdl_studentstatus->upddata();
   }
}

如果要更新单行,此代码段会更新状态为待处理的所有表使用此代码段  控制器

 public function upddata() {
        $this->db->where('studentstatus', 'PENDING');
        $query  =   $this->db->update(studentstatus ,array('studentstatus' => 'ACCEPT'));
        return true;
  }

模型

public function update()
{
   $this->load->model("mdl_studentstatus");
   if ($this->input->post('submit')) {
   $data = array('student_id' => $this->input->post('student_id'));
   $query     = $this->mdl_studentstatus->upddata($data);
  }
}

在您的视图中

public function upddata($data) {
    $this->db->where(array('studentstatus' => 'PENDING' , 'studen_id' => $data['studen_id']));
    $query =   $this->db->update(studentstatus ,array('studentstatus' => 'ACCEPT'));
    return true;
  }