使用Codeigniter更新DB表中的字段

时间:2011-08-20 16:07:09

标签: php codeigniter

我有两个表,一个是'学生',另一个是'状态'。学生表有一个外键status_status_id。我想更新这个status_status_field。在我看来,状态中的所有状态(初级,高级,新鲜,大二)都显示在下拉菜单中。

我想通过从此下拉列表中获取输入来更新student表的status_status_id(最初它具有值0)字段。要更新我还需要特定学生的身份证明。问题是字段status_status_id没有更新。显示以下错误 -

       A Database Error Occurred

        Error Number: 1054

      Unknown column 'Array' in 'where clause'

        UPDATE `student` SET `0` = '' WHERE `id` = Array

       Filename: Z:\www\CI\system\database\DB_driver.php

     Line Number: 330

我的控制器

 function update(){
 $id=$this->input->post('id');
 $data=array(
 'status_status_id'=>$this->input->post('status_status_id'),
 );
 $this->status_model->update($data, $id);
 }
}

我的模特

function update($data,$id){

 $this->db->where('id', $id);
 $this->db->update('student', $data);
}
        }

添加状态的视图

   <?php echo form_open('status_controller/update'); 
    $r=$info[0]->id;
  $data=array (
  'id'=>$r,
 'status_status_id'=>set_value('status_status_id')
           );
          ?>
          <p><?php echo form_input($data); ?>
        <p><select name ='status_status_id'>
       <?php echo form_error('status_status_id'); ?>
        <br /><?php
    $getType = mysql_query("SELECT status_id, status FROM status ORDER BY status_id");
      while($type = mysql_fetch_object($getType)){
    echo "<option value=\"{$type->status_id}\">{$type->status} </option>",set_value('status_status_id');
} ?></p><p><?php echo form_submit( 'submit', 'Update Status'); ?></p><?php echo form_close(); ?>

1 个答案:

答案 0 :(得分:3)

我看到视图错误很少。我只是用评论重写它,它应该可以工作。

请注意,您应该将所有数据库查询移到视图之外,当然还要添加验证。我不确定ID是否实际上是一个整数。

// I suppose this gives us a single ID, not an array of IDs or something else
$r = $info[0]->id;

// put the ID in a hidden field, so POST sends an "id" variable
$hidden_fields = array('id' => $r);

// add the hidden fields directly in form_open()
echo form_open('status_controller/update', '', $hidden_fields); 

// making $getType = mysql_query("SELECT status_id, status FROM status ORDER BY status_id"); in an active record way
$this->db->select('status_id, status');
$this->db->order_by('status_id', 'asc');
$query = $this->db->get('status');

// loop to make an array of the statuses to insert in form_dropdown() array('value' => 'display')
$statuses = array();
foreach ($query->result() as $row)
{
    $statuses[$row->status_id] = $row->status;
}

// you would probably be able to get the current status_status_id
// I will suppose that $info[0]->status_status_id holds that data
echo form_dropdown('status_status_id', $statuses, set_value('status_status_id', $info[0]->status_status_id));
echo form_error('status_status_id');
echo form_submit( 'submit', 'Update Status');
echo form_close();

控制器:

function update(){
    // again, this is suspicious from your example, check if it's not an Array
    $id = $this->input->post('id');
    $data = array(
        'status_status_id' => $this->input->post('status_status_id'),
    );

    $this->status_model->update($data, $id);
}

模特:

function update($data, $id){
    $this->db->where('id', $id);
    $this->db->update('student', $data);
}