Codeigniter - 无法更新MySQL数据库中的数据

时间:2017-11-09 23:08:44

标签: php mysql codeigniter

我对CodeIgniter有一些问题,我无法更新CodeIgniter中记录中的数据。 'id_user'是我的用户表的主键。我试图解决这个问题,但我不能。我希望有人可以帮助解决这个问题。非常感谢:)

我已经发布了以下代码:

HTML5代码:

<form action="<?php echo base_url(). 'crud/update'; ?>" method="post">
    <input name="id_user" type="text" class="form-control" value="<?php echo $id_usernya ?>">
    <div class="form-group">
        <label>Alamat :</label>
        <textarea name="alamat_user" class="form-control" rows="5" placeholder="Alamat" ><?php echo $usernya->alamat_user ?></textarea>
    </div> 
    <div class="form-group">
        <input name="kodepos_user" type="text" class="form-control" value="<?php echo $usernya->kodepos_user ?>">
    </div>
    <div class="form-group">
        <input type="hidden" name="provinsi_user">
        <select name="provinsi_id_user" class="form-control">
            <option value="">Pilih Provinsi</option>
            <option selected value="<?php echo $usernya->provinsi_user ?>"><?php echo $usernya->provinsi_user ?></option>
            <?php echo $provinsi ?>
        </select>
    </div>
    <div class="form-group">
        <input type="hidden" name="kota_user">
        <select name="kota_id_user" class="form-control">
            <option value="">Pilih Kota</option>
            <option selected value="<?php echo $usernya->kota_user ?>"><?php echo $usernya->kota_user ?></option>
        </select>
    </div>
    <input type="submit" class="btn btn-primary" value="Simpan">
</form>

控制器:

class Crud extends CI_Controller{

function __construct(){
    parent::__construct();      
    $this->load->model('m_data');
    $this->load->helper('url');

}

function edit($id_user){
        $where = array('id_user' => $id_user);
        $data['user'] = $this->m_data->edit_data($where,'user')->result();
        $this->load->view('back/user/v_beranda',$data);
        redirect('beranda/index');
    }

    function update(){
    $id_user = $this->input->post('id_user');
    $alamat_user = $this->input->post('alamat_user');
    $kodepos_user = $this->input->post('kodepos_user');
    $provinsi_id_user = $this->input->post('provinsi_id_user');
    $kota_id_user = $this->input->post('kota_id_user');

    $data = array(
        'alamat_user' => $alamat_user,
        'kodepos_user' => $kodepos_user,
        'provinsi_id_user' => $provinsi_id_user,
        'kota_id_user' => $kota_id_user
    );

    $where = array(
        'id_user' => $id_user
    );

    $this->m_data->update_data($where,$data,'user');
    redirect('beranda/index');
}

型号代码:

<?php 

class M_data extends CI_Model{

    function edit_data($where,$table){      
        return $this->db->get_where($table,$where);
    }

    function update_data($where,$data,$table){
        $this->db->where($where);
        $this->db->update($table,$data);
    }   
}

2 个答案:

答案 0 :(得分:0)

初始化您的模型,就像您在控制器上所做的那样

class M_data extends CI_Model{

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function edit_data($where,$table){      
        return $this->db->get_where($table,$where);
    }

    function update_data($where,$data,$table){
        $this->db->where($where);
        $this->db->update($table,$data);
    }  
}

答案 1 :(得分:0)

尝试打印查询以检查执行的确切查询

void printTreeHelper(struct TreeNode* root)
{
  if(root)
  {
    printTreeHelper(root->left);
    printf("%d ", root->data);
    printTreeHelper(root->right);
  }
}

void printTree(struct TreeNode *root)
{
    printTreeHelper(root);
    printf("\n");
}

执行上述查询时显示的错误