php Codeigniter 更新记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4607277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter updating records
提问by CyberJunkie
I'm having some trouble updating records with the Codeigniter framework. I'm using the MVC pattern and active records.
我在使用 Codeigniter 框架更新记录时遇到了一些问题。我正在使用 MVC 模式和活动记录。
The code is for updating user profiles.
该代码用于更新用户配置文件。
In my model, Profile_model
I have a function to update...
在我的模型中,Profile_model
我有一个功能可以更新...
function profile_update()
{
$this->db->where('user_id', 2);
$this->db->update('user_profiles', $data);
}
The controller Profile_crud
should retrieve data from a form and send the data to the model.
控制器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);
}
and the form in my view profile.php
On submit it triggers the update
function in my controller.
和我视图中的表单在profile.php
提交时触发update
我的控制器中的功能。
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(); ?>
When I submit the form I get 3 types of errors.
当我提交表单时,我收到 3 种类型的错误。
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: models/profile_model.php
Line Number: 27
遇到 PHP 错误
严重性:注意
消息:未定义变量:数据
文件名:models/profile_model.php
行号:27
line 27 is $this->db->update('user_profiles', $data);
第 27 行是 $this->db->update('user_profiles', $data);
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home1/requestg/public_html/housedwork/system/libraries/Exceptions.php:166)
Filename: codeigniter/Common.php
Line Number: 356
遇到 PHP 错误
严重性:警告
消息:无法修改标头信息 - 标头已发送(输出开始于 /home1/requestg/public_html/housedwork/system/libraries/Exceptions.php:166)
文件名:codeigniter/Common.php
行号:356
and
和
A Database Error Occurred
You must use the "set" method to update an entry.
发生数据库错误
您必须使用“set”方法来更新条目。
I'm not sure what I'm doing wrong.. Can someone please help?
我不确定我做错了什么..有人可以帮忙吗?
回答by Sarfraz
For your profile_update
function, you are specifying the argument of $data
:
对于您的profile_update
函数,您正在指定 的参数$data
:
$this->Profile_model->profile_update($data);
But in your model function, you have not specified one:
但是在你的模型函数中,你没有指定一个:
function profile_update()
{
$this->db->where('user_id', 2);
$this->db->update('user_profiles', $data);
}
It should be:
它应该是:
function profile_update($data)
{
$this->db->where('user_id', 2);
$this->db->update('user_profiles', $data);
}
回答by bharath
Your profile_update() is missing $data parameter
您的 profile_update() 缺少 $data 参数
function profile_update($data)
{
$this->db->where('user_id', 2);
$this->db->update('user_profiles', $data);
}
回答by Ian Patel
Your model Profile_model
haven't received any parameter.
您的模型Profile_model
尚未收到任何参数。
function profile_update($data) {
return $this->db
->where('user_id', 2)
->update('user_profiles', $data);
}