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

xu3bshqb  于 2023-06-19  发布在  PHP
关注(0)|答案(1)|浏览(147)

有人可以帮助摆脱这个错误,因为我已经陷入了这个错误一段时间,它已经成为非常沮丧,我走出这个错误.错误是:
更新博客
A PHP Error was encountered Severity:警告消息:未定义数组键“blog_id”文件名:Line Number: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');

    }
}

?>
dzjeubhm

dzjeubhm1#

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

你应该打个电话而不是这个

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

相关问题