November 7, 20224 yr hello friends, I was following a youtube video on making a s tudent management system with codeigniter3 but i am stuck at some point when i click on edit on view students page..its showing me the details of same student whatever student i choose here is the code for view viewStudents.php <?php include('inc/header.php');?> <div class="container mt-2"> <h2>Students</h2> <hr> <?php echo anchor('admin/dashboard','BACK',['class'=>'btn btn-warning']);?> <div class="table-responsive"> <table class="table table-hover"> <thead> <tr> <th scope="col">Student Name</th> <th scope="col">College Name</th> <th scope="col">Course</th> <th scope="col">Email</th> <th scope="col">Gender</th> <th scope="col">Action</th> </tr> </thead> <tbody> <?php if(count($students)){ ?> <?php foreach ($students as $student){?> <tr> <td><?php echo $student->studentname; ?></td> <td><?php echo $student->collegename; ?></td> <td><?php echo $student->course; ?></td> <td><?php echo $student->email; ?></td> <td><?php echo $student->gender; ?></td> <td> <?php echo anchor("admin/editStudent/{$student->id}","Edit",['class'=>'btn btn-light']);?> <?php echo anchor("admin/deleteStudent/{$student->id}","Delete",['class'=>'btn btn-danger']);?> </td> </tr> <?php } ?> <?php } else {?> <tr> <td>No Record found</td> </tr> <?php } ?> </tbody> </table> </div> </div> <?php include('inc/footer.php');?> editstudent.php (view) <?php include('inc/header.php');?> <div class="container mt-2"> <?php echo form_open('admin/createStudent',['class'=>'form-hoizontal']);?> <h3 class="text-center display-4">EDIT STUDENT</h3> <hr/> <?php if($msg = $this->session->flashdata('message')){ ?> <div class="row"> <div class="col-md-6"> <div class="alert alert-dismissible fade show alert-success"> <button type="button" class="close" data-dismiss="alert">×</button> <?php echo $msg;?> </div> </div> </div> <?php } ?> <div class="row"> <div class="col-md-6"> <div class="form-group"> <div class="row"> <div class="col-md-3"> <label for="studentname" class="mt-2">Student name:</label> </div> <div class="col-md-9"> <!-- <input type="text" class="form-control" placeholder="User name" id="username"> --> <?php $data = array( 'type' => 'text', 'name' => 'studentname', 'value' => set_value('studentname',$studentData->studentname), 'placeholder' => 'Enter Studentname', 'class' => 'form-control' ); echo form_input($data); ?> </div> </div> </div> </div> <div class="col-md-6"> <?php echo form_error('studentname','<div class="text-danger mt-2">','</div>');?> </div> </div><!--row--> <div class="row"> <div class="col-md-6"> <div class="form-group"> <div class="row mt-3"> <div class="col-md-3"> <label for="email" class="mt-2">College Name:</label> </div> <div class="col-md-9"> <select class="form-control" name="college_id"> <option value="<?php echo $studentData->college_id; ?>"><?php echo $studentData->collegename; ?></option> <?php if(count($colleges)) { ?> <?php foreach ($colleges as $college){?> <option value="<?php echo $college->college_id;?>"><?php echo $college->collegename;?></option> <?php } } ?> </select> </div> </div> </div> </div> <div class="col-md-6"> <?php echo form_error('college_id','<div class="text-danger mt-4">','</div>');?> </div> </div><!--row--> <div class="row"> <div class="col-md-6"> <div class="form-group"> <div class="row mt-3"> <div class="col-md-3"> <label for="email" class="mt-2">Email address:</label> </div> <div class="col-md-9"> <!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> --> <?php $data = array( 'type' => 'email', 'name' => 'email', 'value' => set_value('email'), 'placeholder' => 'Enter Email', 'class' => 'form-control' ); echo form_input($data); ?> </div> </div> </div> </div> <div class="col-md-6"> <?php echo form_error('email','<div class="text-danger mt-4">','</div>');?> </div> </div><!--row--> <div class="row"> <div class="col-md-6"> <div class="form-group"> <div class="row mt-3"> <div class="col-md-3"> <label for="gender" class="mt-2">Gender:</label> </div> <div class="col-md-9"> <!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> --> <select class="form-control" name="gender"> <option value="">Select</option> <option value="Male">Male</option> <option value="Female">Female</option> </select> </div> </div> </div> </div> <div class="col-md-6"> <?php echo form_error('gender','<div class="text-danger mt-4">','</div>');?> </div> </div><!--row--> <div class="row"> <div class="col-md-6"> <div class="form-group"> <div class="row"> <div class="col-md-3"> <label for="course" class="mt-2">Course:</label> </div> <div class="col-md-9"> <!-- <input type="text" class="form-control" placeholder="User name" id="username"> --> <?php $data = array( 'type' => 'text', 'name' => 'course', 'value' => set_value('course'), 'placeholder' => 'Enter Course', 'class' => 'form-control' ); echo form_input($data); ?> </div> </div> </div> </div> <div class="col-md-6"> <?php echo form_error('course','<div class="text-danger mt-2">','</div>');?> </div> </div><!--row--> <div class="row"> <div class="col-md-6"> <!-- <button type="submit" class="btn btn-dark float-right">Register</button> --> <div class="float-right"> <button type="submit" class="btn btn-dark">ADD STUDENT</button> <?php //echo form_submit('Register', 'Register',"class='btn btn-dark'"); ?> <?php echo anchor('welcome','GO BACK',['class'=>'btn btn-warning']);?> </div> </div> </div> <?php echo form_close(); ?> </div> <?php include('inc/footer.php');?> i have joined college table and students table and getting the id of student model queries.php <?php class Queries extends CI_model { public function getRoles() { $roles = $this->db->get('tbl_roles'); if($roles->num_rows() > 0){ return $roles->result(); } } public function getColleges() { $colleges = $this->db->get('tbl_college'); if($colleges->num_rows() > 0){ return $colleges->result(); } } public function registerAdmin($data) { return $this->db->insert('tbl_users',$data); } public function chkAdminExist() { $chkAdmin = $this->db->where(['role_id' => '1']) ->get('tbl_users'); if($chkAdmin->num_rows() > 0){ return $chkAdmin->num_rows(); } } public function adminExist($email,$password) { $chkAdmin = $this->db->where(['email' => $email, 'password' => $password]) ->get('tbl_users'); if( $chkAdmin->num_rows() > 0 ){ return $chkAdmin->row(); } } public function makeCollege($data) { return $this->db->insert('tbl_college',$data); } public function registerCoAdmin($data) { return $this->db->insert('tbl_users',$data); } public function viewAllColleges() { $this->db->select(['tbl_users.user_id','tbl_users.email','tbl_college.college_id','tbl_users.username','tbl_users.gender','tbl_college.collegename','tbl_college.branch','tbl_roles.rolename']); $this->db->from('tbl_college'); $this->db->join('tbl_users', 'tbl_users.college_id = tbl_college.college_id'); $this->db->join('tbl_roles', 'tbl_roles.role_id = tbl_users.role_id'); $users = $this->db->get(); return $users->result(); } public function insertStudent($data) { return $this->db->insert('tbl_students',$data); } public function getStudents($college_id) { $this->db->select(['tbl_students.id','tbl_college.collegename','tbl_students.studentname','tbl_students.email','tbl_students.gender','tbl_students.course']); $this->db->from('tbl_students'); $this->db->join('tbl_college', 'tbl_college.college_id = tbl_students.college_id'); $this->db->where(['tbl_students.college_id' => $college_id]); $students = $this->db->get(); return $students->result(); } //public function getStudentRecord($id) public function getStudentRecord($id) { /* $this->db->select(['tbl_college.college_id','tbl_college.collegename','tbl_students.id','tbl_students.email','tbl_students.gender','tbl_students.studentname','tbl_students.course']); $this->db->from('tbl_students'); $this->db->join('tbl_college', 'tbl_college.college_id = tbl_students.college_id'); */ //$this->db->where(['tbl_students.college_id' => $id]); // $student = $this->db->get(); // return $student->result(); /* $this->db->select(['tbl_college.college_id','tbl_college.collegename','tbl_students.id','tbl_students.email','tbl_students.gender','tbl_students.studentname','tbl_students.course']); */ $this->db->select(['tbl_college.college_id','tbl_college.collegename','tbl_students.id','tbl_students.email','tbl_students.gender','tbl_students.studentname','tbl_students.course']); $this->db->from('tbl_students'); $this->db->join('tbl_college', 'tbl_college.college_id = tbl_students.college_id'); $students = $this->db->get(); return $students->row(); } } ?> here is the code for controller admin.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); // class Welcome extends CI_Controller { class admin extends My_Controller { public function dashboard() { //echo 'dashboard'; /* if(!$this->session->userdata('user_id')){ return redirect('welcome/login'); } */ $this->load->model('queries'); $users = $this->queries->viewAllColleges(); /* echo '<pre>'; print_r($users); echo '</pre>'; exit(); */ $this->load->view('dashboard',['users'=>$users]); } public function addCollege() { //echo 'addcollege'; $this->load->view('addCollege'); } public function addCoadmin() { //echo 'addCoadmin'; $this->load->model('queries'); $roles = $this->queries->getRoles(); $colleges = $this->queries->getColleges(); // print_r($roles); // exit(); $this->load->view('addCoadmin',['roles'=>$roles,'colleges'=>$colleges]); //$this->load->view('addCoadmin'); } public function addStudent() { //echo 'addStudent'; $this->load->model('queries'); $colleges = $this->queries->getColleges(); $this->load->view('addStudent',['colleges'=>$colleges]); //$this->load->view('addStudent'); } public function createCollege() { //echo 'create college'; $this->form_validation->set_rules('collegename','College Name','required'); $this->form_validation->set_rules('branch','Branch','required'); $this->form_validation->set_error_delimiters('<div class="text-danger">','</div>'); if($this->form_validation->run()){ echo 'validation passed'; $data = $this->input->post(); /* $collegename = $this->input->post('collegename'); $branch = $this->input->post('branch');*/ $this->load->model('queries'); //$collegeExist = $this->queries->collegeExist($collgename,$branch); if($this->queries->makeCollege($data)){ $this->session->set_flashdata('message','College Created Successfully'); }else{ $this->session->set_flashdata('message','Failed to Create College '); } return redirect('admin/addCollege'); }else{ //echo validation_errors(); $this->addCollege(); } } public function createCoadmin() { //echo 'create coadmin'; $this->form_validation->set_rules('username','Username','required'); $this->form_validation->set_rules('college_id','College','required'); $this->form_validation->set_rules('email','Email','required'); $this->form_validation->set_rules('gender','Gender','required'); $this->form_validation->set_rules('role_id','Role','required'); $this->form_validation->set_rules('password','Password','required'); $this->form_validation->set_rules('confpwd','Password Again','required|matches[password]'); $this->form_validation->set_error_delimiters('<div class="text-danger">','</div>'); if($this->form_validation->run()){ //echo 'validation passed'; $data = $this->input->post(); $data['password'] =sha1($this->input->post('password')); $data['confpwd'] =sha1($this->input->post('confpwd')); /* echo '<pre>'; print_r($data); echo '</pre>'; exit(); */ $this->load->model('queries'); if($this->queries->registerCoAdmin($data)){ $this->session->set_flashdata('message','Co Admin Registered Successfully'); }else{ $this->session->set_flashdata('message','Failed to Register Co Admin '); } return redirect('admin/addCoadmin'); }else{ //echo 'validation error'; //echo validation_errors(); $this->addCoadmin(); } } public function createStudent() { //echo 'create coadmin'; $this->form_validation->set_rules('studentname','Studentname','required'); $this->form_validation->set_rules('college_id','College Name','required'); $this->form_validation->set_rules('email','Email','required'); $this->form_validation->set_rules('gender','Gender','required'); $this->form_validation->set_rules('course','Course','required'); $this->form_validation->set_error_delimiters('<div class="text-danger">','</div>'); if($this->form_validation->run()){ echo 'validation passed'; $data = $this->input->post(); /*$data['password'] =sha1($this->input->post('password')); $data['confpwd'] =sha1($this->input->post('confpwd')); */ /* echo '<pre>'; print_r($data); echo '</pre>'; exit(); */ $this->load->model('queries'); if($this->queries->insertStudent($data)){ $this->session->set_flashdata('message','Student Added Successfully'); }else{ $this->session->set_flashdata('message','Failed to Add Student '); } return redirect('admin/addStudent'); }else{ //echo 'validation error'; //echo validation_errors(); $this->addStudent(); } } public function viewStudents($college_id) { //echo 'view college'; //echo $college_id; $this->load->model('queries'); $students = $this->queries->getStudents($college_id); $this->load->view('viewStudents',['students'=>$students]); } public function editStudent($id) { $this->load->model('queries'); $studentData = $this->queries->getStudentRecord($id); /* echo '<pre>'; print_r($studentData); echo '</pre>'; exit(); */ $this->load->view('editStudent',['studentData'=>$studentData]); // $this->load->view('editStudent'); //echo 'editStudent'; echo $id; } public function __construct() { parent::__construct(); if(!$this->session->userdata('user_id')){ return redirect('welcome/login'); } } } here is the screenshot of what i see ..when i click edit for different students i have echoed the id of 2 students you can clearly see at the top left but their details ae showing same on the edit page any help will be appreciated..thanks i was missing the where clause in queries.php in getStudentRecord function here is the updated code queries.php <?php class Queries extends CI_model { public function getRoles() { $roles = $this->db->get('tbl_roles'); if($roles->num_rows() > 0){ return $roles->result(); } } public function getColleges() { $colleges = $this->db->get('tbl_college'); if($colleges->num_rows() > 0){ return $colleges->result(); } } public function registerAdmin($data) { return $this->db->insert('tbl_users',$data); } public function chkAdminExist() { $chkAdmin = $this->db->where(['role_id' => '1']) ->get('tbl_users'); if($chkAdmin->num_rows() > 0){ return $chkAdmin->num_rows(); } } public function adminExist($email,$password) { $chkAdmin = $this->db->where(['email' => $email, 'password' => $password]) ->get('tbl_users'); if( $chkAdmin->num_rows() > 0 ){ return $chkAdmin->row(); } } public function makeCollege($data) { return $this->db->insert('tbl_college',$data); } public function registerCoAdmin($data) { return $this->db->insert('tbl_users',$data); } public function viewAllColleges() { $this->db->select(['tbl_users.user_id','tbl_users.email','tbl_college.college_id','tbl_users.username','tbl_users.gender','tbl_college.collegename','tbl_college.branch','tbl_roles.rolename']); $this->db->from('tbl_college'); $this->db->join('tbl_users', 'tbl_users.college_id = tbl_college.college_id'); $this->db->join('tbl_roles', 'tbl_roles.role_id = tbl_users.role_id'); $users = $this->db->get(); return $users->result(); } public function insertStudent($data) { return $this->db->insert('tbl_students',$data); } public function getStudents($college_id) { $this->db->select(['tbl_students.id','tbl_college.collegename','tbl_students.studentname','tbl_students.email','tbl_students.gender','tbl_students.course']); $this->db->from('tbl_students'); $this->db->join('tbl_college', 'tbl_college.college_id = tbl_students.college_id'); $this->db->where(['tbl_students.college_id' => $college_id]); $students = $this->db->get(); return $students->result(); } //public function getStudentRecord($id) public function getStudentRecord($id) { /* $this->db->select(['tbl_college.college_id','tbl_college.collegename','tbl_students.id','tbl_students.email','tbl_students.gender','tbl_students.studentname','tbl_students.course']); $this->db->from('tbl_students'); $this->db->join('tbl_college', 'tbl_college.college_id = tbl_students.college_id'); */ //$this->db->where(['tbl_students.college_id' => $id]); // $student = $this->db->get(); // return $student->result(); /* $this->db->select(['tbl_college.college_id','tbl_college.collegename','tbl_students.id','tbl_students.email','tbl_students.gender','tbl_students.studentname','tbl_students.course']); */ $this->db->select(['tbl_college.college_id','tbl_college.collegename','tbl_students.id','tbl_students.email','tbl_students.gender','tbl_students.studentname','tbl_students.course']); $this->db->from('tbl_students'); $this->db->join('tbl_college', 'tbl_college.college_id = tbl_students.college_id'); $this->db->where(['tbl_students.id' => $id]); $students = $this->db->get(); return $students->row(); } } ?> Edited November 7, 20224 yr by sash_oo7 i got this resolved ..was missing the where clause
Create an account or sign in to comment