更新功能失败并显示“严重性:通知”

发布于 2024-11-03 10:35:39 字数 2786 浏览 1 评论 0原文

我是 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>
    &lt;?php echo form_open('site/update');?&gt;
    <p>
        <label for="update title">Update Title:</label>
        &lt;input type="text" name="updtitle" id="updtitle" /&gt;
    </p>

    <p>
        <label for="update content">Update Content:</label>
        &lt;input type="text" name="updcontent" id="updcontent" /&gt;
    </p>    

    <p>
        &lt;input type="submit" value="Update" /&gt;
    </p>
    &lt;?php echo form_close(); ?&gt;

    <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在看孤独的风景 2024-11-10 10:35:39

您没有在模型调用中定义 $id

function update_record($data) 
{
    $this->db->where('id', $id);// <-- $id is not defined
    $this->db->update('data', $data);
}   

您也没有在控制器调用中使用它:

function update()
{
    $data = array(
        'title' => $this->input->post('updtitle'),
        'content' => $this->input->post('updcontent')
    );

    $this->site_model->update_record($data);
    $this->members_area();

    // Where is id??

}

You didn't define $id in your model call:

function update_record($data) 
{
    $this->db->where('id', $id);// <-- $id is not defined
    $this->db->update('data', $data);
}   

You aren't using it in your Controller call either:

function update()
{
    $data = array(
        'title' => $this->input->post('updtitle'),
        'content' => $this->input->post('updcontent')
    );

    $this->site_model->update_record($data);
    $this->members_area();

    // Where is id??

}
装纯掩盖桑 2024-11-10 10:35:39

我通过重新设计我的应用程序找到了解决方案。

echo-ing $id 和 $data 有帮助。没有输出任何内容,因此没有捕获任何内容。进行更多阅读需要重新设计我的应用程序。

现在,我依赖于要更新的​​记录上的 URI:

1)在我的控制器中,我添加了此更新函数

function update() {
    $id = $this->uri->segment(3);
    $title = $this->input->post('title');
    $content = $this->input->post('content');

    $this->posts_model->update_record($id, $title, $content)
} 

2)在我的模型中,我添加了此函数

function update_record($id, $title, $content) {
    $data = array(
        'title' => $title,
        'content' => $content
    );

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

3)最后,我创建了一个仅用于更新的视图

<?php $this->load->helper('form'); ?>

<?php echo form_open('site/update/'.$id); ?>

        <?php echo form_input('title'); ?>
        <?php echo form_textarea('content'); ?>
        <?php echo form_submit('submit', 'Submit'); ?>

<?php echo form_close(); ? 

它有效现在好了。我所要做的就是将浏览器指向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

function update() {
    $id = $this->uri->segment(3);
    $title = $this->input->post('title');
    $content = $this->input->post('content');

    $this->posts_model->update_record($id, $title, $content)
} 

2) in my model, I added this function

function update_record($id, $title, $content) {
    $data = array(
        'title' => $title,
        'content' => $content
    );

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

3) finally, I created a view just for the update

<?php $this->load->helper('form'); ?>

<?php echo form_open('site/update/'.$id); ?>

        <?php echo form_input('title'); ?>
        <?php echo form_textarea('content'); ?>
        <?php echo form_submit('submit', 'Submit'); ?>

<?php echo form_close(); ? 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文