php - Trying to get property 'studentnumber' of non-object codeigniter edit_student
one text
error:
A PHP Error was encountered Severity: Notice
Message: Trying to get property 'studentnumber' of non-object
Filename: users/edit_student.php
Line Number: 21
Backtrace:
File: C:\UniServerZ\www\student_monitoring_responsive\application\views\users\edit_student.php Line: 21 Function: _error_handler
File: C:\UniServerZ\www\student_monitoring_responsive\application\controllers\Students.php Line: 110 Function: view
File: C:\UniServerZ\www\student_monitoring_responsive\index.php Line: 315 Function: require_once
Here is my code: controllers
<?php
class Students extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('students_model');
}
public function students()
{
if(!$this->session->userdata('id'))
redirect('login/index');
$this->load->view('templates/header');
$data['students_info'] = $this->students_model->rows();
$this->load->view('users/students', $data);
$this->load->view('templates/footer');
}
public function edit_student($id)
{
if(!$this->session->userdata('id'))
redirect('login/index');
$this->form_validation->set_rules('studentno', 'Student Number', 'trim|required');
$this->form_validation->set_rules('fname', 'First Name', 'trim|required');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
$this->form_validation->set_rules('contact', 'Contact', 'trim|required');
$this->form_validation->set_rules('institution', 'Institution', 'trim|required');
$this->form_validation->set_rules('pgrade', 'Prelim Grade', 'trim|required');
$this->form_validation->set_rules('mgrade', 'Midterm Grade', 'trim|required');
$this->form_validation->set_rules('fgrade', 'Final Grade', 'trim|required');
$this->form_validation->set_rules('agrade', 'Average Grade', 'trim|required');
if ($this->form_validation->run()) {
$data = array(
'studentnumber' => $this->input->post('studentno'),
'firstname' => $this->input->post('fname'),
'middlename' => $this->input->post('mname'),
'lastname' => $this->input->post('lname'),
'address' => $this->input->post('address'),
'contact' => $this->input->post('contact'),
'institution' => $this->input->post('institution'),
'pgrade' => $this->input->post('pgrade'),
'mgrade' => $this->input->post('mgrade'),
'fgrade' => $this->input->post('fgrade'),
'agrade' => $this->input->post('agrade'),
);
$result = $this->students_model->edit_student($id, $data);
if ($result) {
$this->students();
}
} else {
$this->load->view('templates/header');
$data['student_info'] = $this->students_model->row($id);
$this->load->view('users/edit_student', $data);
$this->load->view('templates/footer');
}
}
}
?>
Here is my code: models
<?php
class Students_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->table = 'students';
}
public function row($id)
{
$where = array(
'id' => $id,
'is_deleted' => 0
);
$query = $this->db->get_where($this->table, $where);
return $query->row();
}
public function edit_student($id, $data)
{
$where = array(
'id' => $id,
'is_deleted' => 0
);
$this->db->update($this->table, $data, $where);
return true;
}
}
Here is my code: html
<title>Update Student | Student Monitoring</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>/css/edit.css">
</div>
</nav>
</article>
<main class="articlemain">
<div class="container-fluid">
<article class="art1">
<div class="divheader">
<b>Update Information</b>
</div>
<form action="" method="post">
<?= form_open('students/students', ['name' => 'studenttedit', 'autocomplete' => 'off']); ?>
<?php
if (validation_errors()) {
echo validation_errors();
}
?>
<div class="form-group">
<label><b>Student Number*</b></label>
<?php echo form_input(['name' => 'studentno', 'class' => 'form-control', 'value' => $student_info->studentnumber]); ?>
<?php echo form_error('studentno', "<div style='color:red'>", "</div>"); ?>
<label><b>First Name*</b></label>
<?php echo form_input(['name' => 'fname', 'class' => 'form-control', 'value' => $student_info->firstname]); ?>
<?php echo form_error('fname', "<div style='color:red'>", "</div>"); ?>
<label><b>Middle Name</b></label>
<?php echo form_input(['name' => 'mname', 'class' => 'form-control', 'value' => $student_info->middlename]); ?>
<?php echo form_error('mname', "<div style='color:red'>", "</div>"); ?>
<label><b>Last Name</b></label>
<?php echo form_input(['name' => 'lname', 'class' => 'form-control', 'value' => $student_info->lastname]); ?>
<?php echo form_error('lname', "<div style='color:red'>", "</div>"); ?>
<label><b>Address*</b></label>
<?php echo form_input(['name' => 'address', 'class' => 'form-control', 'value' => $student_info->address]); ?>
<?php echo form_error('address', "<div style='color:red'>", "</div>"); ?>
<label><b>Contact*</b></label>
<?php echo form_input(['name' => 'contact', 'class' => 'form-control', 'value' => $student_info->contact]); ?>
<?php echo form_error('contact', "<div style='color:red'>", "</div>"); ?>
<label><b>Institution*</b></label>
<?php echo form_input(['name' => 'institution', 'class' => 'form-control', 'value' => $student_info->institution]); ?>
<?php echo form_error('institution', "<div style='color:red'>", "</div>"); ?>
<label><b>Prelim Grade*</b></label>
<?php echo form_input(['name' => 'pgrade', 'class' => 'form-control', 'value' => $student_info->pgrade]); ?>
<?php echo form_error('pgrade', "<div style='color:red'>", "</div>"); ?>
<label><b>Midterm Grade*</b></label>
<?php echo form_input(['name' => 'mgrade', 'class' => 'form-control', 'value' => $student_info->mgrade]); ?>
<?php echo form_error('mgrade', "<div style='color:red'>", "</div>"); ?>
<label><b>Final Grade*</b></label>
<?php echo form_input(['name' => 'fgrade', 'class' => 'form-control', 'value' => $student_info->fgrade]); ?>
<?php echo form_error('fgrade', "<div style='color:red'>", "</div>"); ?>
<label><b>Average Grade*</b></label>
<?php echo form_input(['name' => 'agrade', 'class' => 'form-control', 'value' => $student_info->agrade]); ?>
<?php echo form_error('agrade', "<div style='color:red'>", "</div>"); ?>
</div>
<?php echo form_submit(['name' => 'submit', 'value' => 'Update', 'class' => 'btn btn-primary']); ?>
<?php echo form_close(); ?>
</form>
</article>
</div>
Source