Codeigniter更新记录

Cyb*_*kie 4 php mysql database codeigniter

我在使用Codeigniter框架更新记录时遇到了一些麻烦.我正在使用MVC模式和活动记录.

该代码用于更新用户配置文件.

在我的模型中,Profile_model我有一个更新功能...

function profile_update() 
    {
        $this->db->where('user_id', 2);
        $this->db->update('user_profiles', $data);              
    }
Run Code Online (Sandbox Code Playgroud)

控制器Profile_crud应从表单中检索数据并将数据发送到模型.

function update() 
    {
        $data = array (
            'country' => $this->input->post('country'),
            'website' => $this->input->post('website')         
        );

        $this->load->model('Profile_model');
        $this->Profile_model->profile_update($data);
    }
Run Code Online (Sandbox Code Playgroud)

在我的视图中的表单profile.php提交它会触发update我的控制器中的功能.

更新

<?php echo form_open('profile_crud/update'); ?>

<p>
<label for="country">Country</label>
<input type="text" name="country" id="country" />
</p>
<p>
<label for="website">Website</label>
<input type="text" name="website" id="website" />
</p>

<p><input type="submit" value="Save" /></p>

<?php echo form_close(); ?>
Run Code Online (Sandbox Code Playgroud)

当我提交表单时,我得到3种类型的错误.

遇到PHP错误

严重性:注意

消息:未定义的变量:数据

文件名:models/profile_model.php

行号:27

第27行是 $this->db->update('user_profiles', $data);

遇到PHP错误

严重性:警告

消息:无法修改标头信息 - 已经发送的标头(输出从/home1/requestg/public_html/housedwork/system/libraries/Exceptions.php:166开始)

文件名:codeigniter/Common.php

行号:356

发生数据库错误

您必须使用"set"方法更新条目.

我不确定我做错了什么..有人可以帮忙吗?

Sar*_*raz 15

对于您的profile_update函数,您指定的参数为$data:

$this->Profile_model->profile_update($data);
Run Code Online (Sandbox Code Playgroud)

但是在你的模型函数中,你没有指定一个:

function profile_update() 
{
    $this->db->where('user_id', 2);
    $this->db->update('user_profiles', $data);              
}
Run Code Online (Sandbox Code Playgroud)

它应该是:

function profile_update($data) 
{
    $this->db->where('user_id', 2);
    $this->db->update('user_profiles', $data);              
}
Run Code Online (Sandbox Code Playgroud)