在 CodeIgniter 的 editblog.php 视图页面中收到“未定义的数组键”警告 - 如何解决?

问题描述 投票:0回答:1

有人可以帮助我摆脱这个错误吗,因为我已经陷入这个错误一段时间了,我对摆脱这个错误感到非常沮丧。 错误是:

更新博客

遇到 PHP 错误 严重性:警告 消息:未定义的数组键“blog_id” 文件名:博客/editblog.php 行数:8

这是我的editblog.php查看页面

<?php $this->load->view('Admin/header'); ?>


<div class="col-md-10 col-sm-10 offset-md-2">
   
    <h3 class="ms-5">Update Blog</h3>
    <br>
    <form action="<?php echo base_url('index.php/Blog/update_blog/') .$blog['blog_id'];?>" method="POST">
        <div class="form-group">
            <b><label for="title" class="ms-5">Title:</label></b>
            <input type="text" class="form-control mx-5"  placeholder="enter blog title" name="title" value="<?= set_value('title',$blog['title'])?>">
            <p ><?= form_error('title');?></p>
        </div>
        <br>

        <!-- by this for the form we can populate the data -->
        <div class="form-group">
            <b><label for="description" class="ms-5">Description:</label></b>
            <?php $textarea = array(
                'name'=>'desc',
                'id' =>'description',
                'value'=>set_value('desc',$blog['desc']),
                'rows'=>'5',
                'cols'=>'5',
                'class'=>'form-control mx-5'
            );
            echo form_textarea($textarea); ?>
            <p ><?= form_error('desc');?></p>

        </div>

        <br>    
        <div class="form-group">
            <b><label for="author" class="ms-5">Author:</label></b>
            <input type="text" class="form-control mx-5" name="author" value="<?= set_value('author',$blog['author']);?>" placeholder="Author of the blog">
            <p><?= form_error('author');?></p>

        </div>
        <br>

        <button class="btn btn-primary mx-5" name="submit" type="submit">Update</button>

    </form>

</div>



<?php $this->load->view('Admin/footer'); ?>

这是我的控制器:

public function update_blog($blog_id)
{

    $this->load->model('blogmodel');

    $dataarr = $this->blogmodel->getdata($blog_id);
    $datalist = array();

    $datalist['blog'] = $dataarr;

    $this->load->library('form_validation');
    $this->form_validation->set_rules('title', 'Title of the blog', 'trim|required');
    $this->form_validation->set_rules('desc', 'description of the blog', 'trim|required');
    $this->form_validation->set_rules('author', 'author of the blog', 'trim|required');

    if ($this->form_validation->run() == false) {

        $this->load->view('Admin/blog/editblog', $datalist);
    } else {

        $data = array();
        $data['title'] = $this->input->post('title');
        $data['desc'] = $this->input->post('desc');
        $data['author'] = $this->input->post('author');
        $data['created_at'] = date('Y-m-d');
        $this->blogmodel->edit($blog_id, $data);
        $this->session->set_flashdata('success', 'Blog updated successfully');
        redirect('Blog/bloglist');
    }
}

下面是我的模型 blog_model.php

<?php 

class blogmodel extends CI_Model {

    public function Add($formArray) {

        $this->db->insert('blogs',$formArray);
    }

    //fetching the all blogs records

    public function getAllrecords() {
      return $blogs = $this->db->get('blogs')->result_array();
    }

    //updating the blogs

    public function edit($blog_id,$data) {
        $this->db->where('blog_id',$blog_id);
        $this->db->update('blogs',$data);
    }

    //fetching one record with blogid

    public function getdata($blog_id) {
        $this->db->where('blog_id',$blog_id);
       $result = $this->db->get('blogs')->result_array();
       return $result;
    }

    function delete_blog($blog_id) {

        $this->db->where('blog_id',$blog_id);
        $this->db->delete('blogs');

    }
}


?>
javascript php .htaccess codeigniter codeigniter-3
1个回答
0
投票

双重确认; 控制器函数 update_blog($blog_id) $blog_id 存在于数据库表中并传递给该函数,

并且获取结果不是空数组

© www.soinside.com 2019 - 2024. All rights reserved.