这里是我的代码,当我在做更新代码时,我每次都收到相同的消息 - "未定义的变量" 。请帮我找到问题所在。我最近2天无法解决问题。
错误消息 -
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: department
Filename: medical_diagnosis/test_department_form.php
Line Number: 17
Backtrace:
File: /home/qsystems/public_html/hospital/application/views/medical_diagnosis/test_department_form.php
Line: 17
Function: _error_handler
File: /home/qsystems/public_html/hospital/application/controllers/medical_diagnosis/Test_department.php
Line: 26
Function: view
File: /home/qsystems/public_html/hospital/index.php
Line: 315
Function: require_once
我正在分享我的观点,控制器和模型。
查看
这是我的查看文件test_department_form.php
<div class="row">
<!-- form area -->
<div class="col-sm-12">
<div class="panel panel-default thumbnail">
<div class="panel-heading no-print">
<div class="btn-group">
<a class="btn btn-primary" href="<?php echo base_url("medical_diagnosis/test_department") ?>"> <i class="fa fa-list"></i> <?php echo display('medical_diagnosis_test_dept_lst') ?> </a>
</div>
</div>
<div class="panel-body panel-form">
<div class="row">
<div class="col-md-9 col-sm-12">
<?php echo form_open('medical_diagnosis/test_department/create','class="form-inner"') ?>
<?php echo form_hidden('id',$department->id) ?>
<div class="form-group row">
<label for="name" class="col-xs-3 col-form-label"><?php echo display('department_name') ?> <i class="text-danger">*</i></label>
<div class="col-xs-9">
<input name="name" type="text" class="form-control" id="name" placeholder="<?php echo display('department_name') ?>" value="<?php echo $department->name ?>">
</div>
</div>
<!--Radio-->
<div class="form-group row">
<label class="col-sm-3"><?php echo display('status') ?></label>
<div class="col-xs-9">
<div class="form-check">
<label class="radio-inline"><input type="radio" name="status" value="1" checked><?php echo display('active') ?></label>
<label class="radio-inline"><input type="radio" name="status" value="0"><?php echo display('inactive') ?></label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-3 col-sm-6">
<div class="ui buttons">
<button type="reset" class="ui button"><?php echo display('reset') ?></button>
<div class="or"></div>
<button class="ui positive button"><?php echo display('save') ?></button>
</div>
</div>
</div>
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
</div>
&#13;
控制器
这是我的Controllerfile Test_department.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test_department extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model(array(
'medical_diagnosis/test_department_model'
));
if ($this->session->userdata('isLogIn') == false
|| $this->session->userdata('user_role') != 1
)
redirect('login');
}
public function index()
{
$data['title'] = display('medical_diagnosis_test_dept');
#-------------------------------#
$data['departments'] = $this->test_department_model->read();
$data['content'] = $this->load->view('medical_diagnosis/test_department_form',$data,true);
$this->load->view('layout/main_wrapper',$data);
}
public function create()
{
$data['title'] = display('medical_diagnosis_test_dept_add');
#-------------------------------#
$this->form_validation->set_rules('name', display('department_name') ,'required');
$this->form_validation->set_rules('status', display('status') ,'required');
#-------------------------------#
$data['department'] = (object)$postData = [
'id' => $this->input->post('id',true),
'name' => $this->input->post('name',true),
'status' => $this->input->post('status',true)
];
#-------------------------------#
if ($this->form_validation->run() === true) {
#if empty $dprt_id then insert data
if (empty($postData['id'])) {
if ($this->test_department_model->create($postData)) {
#set success message
$this->session->set_flashdata('message', display('save_successfully'));
} else {
#set exception message
$this->session->set_flashdata('exception',display('please_try_again'));
}
redirect('medical_diagnosis/test_department/create');
} else {
if ($this->test_department_model->update($postData)) {
#set success message
$this->session->set_flashdata('message', display('update_successfully'));
} else {
#set exception message
$this->session->set_flashdata('exception',display('please_try_again'));
}
redirect('test_department/edit/'.$postData['id']);
}
} else {
$data['content'] = $this->load->view('medical_diagnosis/test_department_form',$data,true);
$this->load->view('layout/main_wrapper',$data);
}
}
public function edit($id = null)
{
$data['title'] = display('department_edit');
#-------------------------------#
$data['department'] = $this->test_department_model->read_by_id($id);
$data['content'] = $this->load->view('medical_diagnosis/test_department_form',$data,true);
$this->load->view('layout/main_wrapper',$data);
}
}
&#13;
型号
这是我的模型Test_department_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test_department_model extends CI_Model {
private $table = 'diagnosis_test_department';
public function create($data = [])
{
return $this->db->insert($this->table,$data);
}
public function read()
{
return $this->db->select("*")
->from($this->table)
->order_by('id','desc')
->get()
->result();
}
public function read_by_id($id = null)
{
return $this->db->select("*")
->from($this->table)
->where('id',$id)
->get()
->row();
}
public function update($data = [])
{
return $this->db->where('id',$data['id'])
->update($this->table,$data);
}
}
&#13;
答案 0 :(得分:0)
原因很简单......
如果你创建了一个新部门,那么你根本就没有department
对象。
所以非常明显的解决方案是, 而不是
<?php echo form_hidden('id',$department->id) ?>
你写了
<?php
if (isset($department) && $department->id > 0)
{
echo form_hidden('id',$department->id);
}
?>