Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从数据库中获取一行并通过codeigniter中的控制器将其传递给视图_Php_Codeigniter - Fatal编程技术网

Php 如何从数据库中获取一行并通过codeigniter中的控制器将其传递给视图

Php 如何从数据库中获取一行并通过codeigniter中的控制器将其传递给视图,php,codeigniter,Php,Codeigniter,我需要从数据库中获取一行,并且必须通过控制器将该行传递到视图中 function delete_student() { $student_id=$this->input->post('student_id'); $class=$this->input->post('class'); $this->admin_model->delete_student($student_id); $list["status"]="deleted"

我需要从数据库中获取一行,并且必须通过控制器将该行传递到视图中

function delete_student()
{
    $student_id=$this->input->post('student_id');
    $class=$this->input->post('class');
    $this->admin_model->delete_student($student_id);
    $list["status"]="deleted";
    $list["class"]=$class;
    $list["classes"]=$this->admin_model->class_list();
    $list["students"]=$this->admin_model->student_list($class);
    $this->load->view('admin/add_student',$list);
}
我的管理员模型代码是

function delete_student($student_id)
{
    $this->db->where('id',$student_id);
    $this->db->delete('student');

    $this->db->where('id',$student_id);
    $query=$this->db->get('student');
    $result=$query->row_array();
    $row=$result[0];

    $father=$row['father_name'];
    $mother=$row['mother_name'];
    $this->db->where(array('father_name'=>$father,'mother_name'=>$mother));
    $this->db->delete('parent');

}
和控制器

function delete_student()
{
    $student_id=$this->input->post('student_id');
    $class=$this->input->post('class');
    $this->admin_model->delete_student($student_id);
    $list["status"]="deleted";
    $list["class"]=$class;
    $list["classes"]=$this->admin_model->class_list();
    $list["students"]=$this->admin_model->student_list($class);
    $this->load->view('admin/add_student',$list);
}
在这里,学生被删除了。我想在调用函数delete_student()时删除要删除的相应父级

它显示了错误

严重性:通知

消息:未定义的偏移量:0


文件名:models/admin_model.php

在admin_model中有一些错误的代码

检查:

首先,删除学生id匹配的记录:

$this->db->where('id',$student_id);
$this->db->delete('student');
再次检索记录之后:

$this->db->where('id',$student_id);
$query=$this->db->get('student');
$result=$query->row_array();
现在,您的
$result
为空,因为记录已被删除

因此,由于语句上的索引无效而生成错误:

 $row=$result[0];

尝试将
if(!empty($result))
放在
$row=$result[0]之前并检查结果。

在此处打印您的结果我添加了显示未定义偏移量的结果:0然后我可以先删除父项,然后删除学生,如$this->db->where('id',$student\u id);$query=$this->db->get('student');$result=$query->row_array();$row=$result[0];$father=$row['father_name'];$mother=$row['mother_name'];$this->db->where(数组('father\u name'=>$father,'mother\u name'=>$mother));$this->db->delete('parent');$this->db->where('id',$student\u id);$this->db->delete('student');是的,试试看。您需要在删除记录之前检索该记录。我添加了u建议的函数,现在错误不存在了。非常感谢,现在我想删除家长也。是否需要从控制器调用另一个函数来执行此操作?无需。只需使用一个函数即可。