更新功能失败并显示“严重性:通知”
我是 PHP 和 CodeIgniter 的新手。我正在构建一个 CRUD 应用程序,它在更新操作方面运行良好。
我的控制器:
class Site extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function members_area()
{
$data = array();
if($query = $this->site_model->get_records()) // site model is autoloaded.
{
$data['records'] = $query;
}
$this->load->view('members_area', $data);
}
function create()
{
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->members_area();
}
function delete()
{
$this->site_model->delete_row();
$this->members_area();
}
function update()
{
$data = array(
'title' => $this->input->post('updtitle'),
'content' => $this->input->post('updcontent')
);
$this->site_model->update_record($data);
$this->members_area();
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
$this->load->view('login_form');
}
}
}
我的模型:
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('data');
return $query->result();
}
function add_record($data)
{
$this->db->insert('data', $data);
return;
}
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('data');
}
function update_record($data)
{
$this->db->where('id', $id);
$this->db->update('data', $data);
}
我的视图:
<h2>update</h2>
<?php echo form_open('site/update');?>
<p>
<label for="update title">Update Title:</label>
<input type="text" name="updtitle" id="updtitle" />
</p>
<p>
<label for="update content">Update Content:</label>
<input type="text" name="updcontent" id="updcontent" />
</p>
<p>
<input type="submit" value="Update" />
</p>
<?php echo form_close(); ?>
<hr />
如果我尝试更新数据库中的记录,我的模型中会出现错误(“严重性:通知”,“消息:未定义的变量:id”),并且更新不会发生。
我缺少什么?你能帮忙吗?谢谢。
I am a newbie with PHP and CodeIgniter. I am building a CRUD app that works well expect for the update operation.
My controller:
class Site extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function members_area()
{
$data = array();
if($query = $this->site_model->get_records()) // site model is autoloaded.
{
$data['records'] = $query;
}
$this->load->view('members_area', $data);
}
function create()
{
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->members_area();
}
function delete()
{
$this->site_model->delete_row();
$this->members_area();
}
function update()
{
$data = array(
'title' => $this->input->post('updtitle'),
'content' => $this->input->post('updcontent')
);
$this->site_model->update_record($data);
$this->members_area();
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
$this->load->view('login_form');
}
}
}
My model:
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('data');
return $query->result();
}
function add_record($data)
{
$this->db->insert('data', $data);
return;
}
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('data');
}
function update_record($data)
{
$this->db->where('id', $id);
$this->db->update('data', $data);
}
My view:
<h2>update</h2>
<?php echo form_open('site/update');?>
<p>
<label for="update title">Update Title:</label>
<input type="text" name="updtitle" id="updtitle" />
</p>
<p>
<label for="update content">Update Content:</label>
<input type="text" name="updcontent" id="updcontent" />
</p>
<p>
<input type="submit" value="Update" />
</p>
<?php echo form_close(); ?>
<hr />
If I attempt to update a record in my db, I get an error in my model ("Severity: Notice", "Message: Undefined variable: id") and the update does NOT happen.
What am I missing? Can you please help? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有在模型调用中定义
$id
:您也没有在控制器调用中使用它:
You didn't define
$id
in your model call:You aren't using it in your Controller call either:
我通过重新设计我的应用程序找到了解决方案。
echo-ing $id 和 $data 有帮助。没有输出任何内容,因此没有捕获任何内容。进行更多阅读需要重新设计我的应用程序。
现在,我依赖于要更新的记录上的 URI:
1)在我的控制器中,我添加了此更新函数
2)在我的模型中,我添加了此函数
3)最后,我创建了一个仅用于更新的视图
它有效现在好了。我所要做的就是将浏览器指向site/update/post-id_number。
我需要弄清楚如何定制界面,以便我可以将更新视图与我拥有的另一个视图合并。
感谢您抽出时间。
I figured out a solution by re-working my app.
echo-ing $id and $data helped. Nothing was being output therefore nothing was being captured. Doing more reading got to redesign my app.
Now, I rely on a URI on the record I want to update:
1) in my controller, I added this update function
2) in my model, I added this function
3) finally, I created a view just for the update
It works fine now. All I have to do is point my browser to site/update/post-id_number.
What I need to figure out is how to tailor the interface so I can get merge the update view with the other one I have.
Thanks for taking the time.