Unable to update or delete rows in database using codeigniter - php

I am new to code-igniter, and I am facing unknown issues in updation and deletion of rows in my database. My code for Controller is :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Nhome extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->Model('N_model');
$data['r'] = $this->N_model->getdata();
$this->load->view('Homeview',$data);
}
public function edit()
{
$id = $this->input->get('id');
$this->load->Model('N_model');
$data['s'] = $this->N_model->editdata();
$this->load->view('Neditview',$data);
}
public function loadEdit()
{
$id = $this->input->get('id');
$this->load->view('Neditview');
}
public function insertdata()
{
$eID = isset($_POST['Id'])?$_POST['Id']:'';
$arr['Name'] = $_POST['Name'];
$arr['Gender'] = $_POST['Gender'];
$arr['Email'] = $_POST['Email'];
$this->load->Model('N_model');
$res = $this->N_model->updatedata($arr , $eID);
if($res){
header('location:'.base_url()."index.php/Nhome/".$this->index());
}
}
public function delete(){
$this->load->Model('N_model');
$id = $this->input->get('Id');
$this->N_model->deletedata($id);
$this->index();
}
}
and my code for model is :
<?php
class N_Model extends CI_Model{
public $Id;
public $Name;
public $Gender;
public $Email;
public function __construct()
{
parent::__construct();
}
public function getdata()
{
$va = $this->db->get('newprac');
$res = $va->result();
return $res;
}
public function editdata($id)
{
$vr = $this->db->where('Id',$id);
return $vr;
}
public function updatedata($data , $id){
$this->db->where('newprac.Id',$id);
$res = $this->db->update('newprac', $data);
return $res;
}
public function deletedata($id)
{
$this->db->where('newprac.id',$id);
$this->db->delete('newprac');
if($this->db->affected_rows()>0)
{
return true;
}
else { return false; }
}
}

Change
$this->db->where('newprac.id',$id)
to
$this->db->where('id',$id);

Simplify your code to:
$this->db->where('id',$id)->update('newprac', $data);

Replace your controller by this code. Becuase I think you passing data as a post method so you should use post when you retrieve data.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Nhome extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->Model('N_model');
$data['r'] = $this->N_model->getdata();
$this->load->view('Homeview',$data);
}
public function edit()
{
$id = $this->input->post('id');
$this->load->Model('N_model');
$data['s'] = $this->N_model->editdata();
$this->load->view('Neditview',$data);
}
public function loadEdit()
{
$id = $this->input->post('id');
$this->load->view('Neditview');
}
public function insertdata()
{
$eID = isset($_POST['Id'])?$_POST['Id']:'';
$arr['Name'] = $_POST['Name'];
$arr['Gender'] = $_POST['Gender'];
$arr['Email'] = $_POST['Email'];
$this->load->Model('N_model');
$res = $this->N_model->updatedata($arr , $eID);
if($res){
header('location:'.base_url()."index.php/Nhome/".$this->index());
}
}
public function delete(){
$this->load->Model('N_model');
$id = $this->input->post('Id');
$this->N_model->deletedata($id);
$this->index();
}
}

Did you try to debug using Chrome debugger. You might get the exact error in debugger. I suggest you to try once let me know the error name.

Related

My CodeIgniter PHP code cannot update the data

I have the code like below, but when I update the data, the data is not updated but add new data in the database
How can I make updates / edit data and not add new data when saved?
Here I use CodeIgniter version 3.1.8, php version 7.2.4
model_anggota.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_anggota extends CI_model {
public function getdata($key){
$this->db->where('nama',$key);
$hasil = $this->db->get('tb_anggota');
return $hasil;
}
public function getupdate($key,$data){
$this->db->where('nama',$key);
$this->db->update('tb_anggota',$data);
}
public function getinsert($data){
$this->db->insert('tb_anggota',$data);
}
public function getdelete($key){
$this->db->where('nim',$key);
$this->db->delete('tb_anggota');
}
}
data_anggota.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Data_anggota extends CI_Controller {
public function index()
{
$this->model_security->getsecurity();
$isi['content'] = 'data_anggota/tampil_dataanggota';
$isi['data'] = $this->db->get('tb_anggota');
$this->load->view('tampilan_home',$isi);
}
public function tambah(){
$this->model_security->getsecurity();
$isi['content'] = 'data_anggota/form_tambahanggota';
$this->load->view('tampilan_home',$isi);
}
public function edit(){
$this->model_security->getsecurity();
$isi['content'] = 'data_anggota/form_editanggota';
$key = $this->uri->segment(3);
$this->db->where('nim',$key);
$query = $this->db->get('tb_anggota');
if($query->num_rows()>0){
foreach ($query->result() as $row) {
$isi['nama'] = $row->nama;
$isi['nim'] = $row->nim;
$isi['jurusan'] = $row->jurusan;
$isi['tempat_lahir'] = $row->tempat_lahir;
$isi['tanggal_lahir'] = $row->tanggal_lahir;
$isi['alamat'] = $row->alamat;
$isi['agama'] = $row->agama;
$isi['kelamin'] = $row->kelamin;
$isi['angkatan'] = $row->angkatan;
$isi['status'] = $row->status;
}
}
$this->load->view('tampilan_home',$isi);
}
public function simpan(){
$this->model_security->getsecurity();
$key = $this->input->post('nim');
$data['nama'] = $this->input->post('nama');
$data['nim'] = $this->input->post('nim');
$data['jurusan'] = $this->input->post('jurusan');
$data['tempat_lahir'] = $this->input->post('tempat_lahir');
$data['tanggal_lahir'] = $this->input->post('tanggal_lahir');
$data['alamat'] = $this->input->post('alamat');
$data['agama'] = $this->input->post('agama');
$data['kelamin'] = $this->input->post('kelamin');
$data['angkatan'] = $this->input->post('angkatan');
$data['status'] = $this->input->post('status');
$this->load->model('model_anggota');
$query = $this->model_anggota->getdata($key);
if($query->num_rows()>0){
$this->model_anggota->getupdate($key,$data);
$this->session->set_flashdata('info','Data sukses diupdate');
}else{
$this->model_anggota->getinsert($data);
$this->session->set_flashdata('info','Data sukses disimpan');
}
redirect('data_anggota');
}
public function delete(){
$this->model_security->getsecurity();
$this->load->model('model_anggota');
$key = $this->uri->segment(3);
$this->db->where('nim',$key);
$query = $this->db->get('tb_anggota');
if($query->num_rows()>0){
$this->model_anggota->getdelete($key);
$this->session->set_flashdata('info','Data sukses dihapus');
}
redirect('data_anggota');
}
}
The problem is you are getting nim value as $key, and sending that to check in the name column. so, it's returning 0. as per your code, if you the 0 return data will be inserted instead of update. as well as in the update part also, you are checking nim value in name field.
update your codes like following
public function getdata($key){
$this->db->where('nim',$key);
$hasil = $this->db->get('tb_anggota');
return $hasil;
}
public function getupdate($key,$data){
$this->db->where('nim',$key);
$this->db->update('tb_anggota',$data);
}

codeigniter 3: does my code follow MVC rule

I am new to codeigniter 3 and try to convert my PHP project to MVC by this Framework
Controller file
class home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('frontend/M_Headers');
}
public function index()
{
$data['slide_image'] = $this->M_Headers->get_all_slide();
$this->load->view('frontend/headers',$data);
}
}
Model file
class M_Headers extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_all_slide()
{
$query = $this->db->get('tbl_slide');
return $query->result();
}
}
View file
<div class="slider">
<ul class="rslides" id="slider">
<?php
if(count($slide_image) > 0)
{
foreach($slide_image as $value):
$get_image=$value->sl_image;
if($get_image != ''):
$image_properties = array(
'src' => 'assets/images/slide/'.$get_image,
'alt' => '',
);
?>
<li><?php echo img($image_properties); ?></li>
<?php else: ?>
<li>No Slide</li>
<?php
endif;
endforeach;
}
?>
</ul>
</div>
did am i right that loop result in View or should i do it in
Controller?
which query should i use between active record or bind (where id=?) or they had it own benefit in difference situation?
The loop in the view is correct. Because it is "view logic" (repeating li's) you can use into the view.
Your Model can be better. I personally like to use the model as a representation of the item from the database. Like this:
class M_Headers extends CI_Model
{
private var $tablename = "tbl_slide";
var $id;
var $sl_image;
public function __construct()
{
parent::__construct();
}
public function get_all_slide()
{
$slides = array();
$query = $this->db->get($this->tablename);
foreach($query->result() as $row)
{
$item = new self();
$item->id = $row->id;
$item->sl_image = $row->sl_image;
$slides[] = $item;
}
return $slides;
}
public function get_slide($id)
{
$this->db->where("id", $id);
$query = $this->db->get($this->tablename);
$results = $query->result();
if(isset($results[0]))
{
$row = $results[0];
$this->id = $row->id;
$this->sl_image = $row->sl_image;
return true;
}
return false;
}
}
You can even improve this to make a method that loads the row into the model:
class M_Headers extends CI_Model
{
private var $tablename = "tbl_slide";
var $id;
var $sl_image;
public function __construct()
{
parent::__construct();
}
private function load_with_record($row)
{
$this->id = $row->id;
$this->sl_image = $row->sl_image;
}
public function get_all_slide()
{
$slides = array();
$query = $this->db->get($this->tablename);
foreach($query->result() as $row)
{
$item = new self();
$item->load_with_row($row);
$slides[] = $item;
}
return $slides;
}
public function get_slide($id)
{
$this->db->where("id", $id);
$query = $this->db->get($this->tablename);
$results = $query->result();
if(isset($results[0]))
{
$row = $results[0];
$this->load_with_row($row);
return true;
}
return false;
}
}

A PHP Error was encountered Severity: Notice Message: Undefined index: questions

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class checklist extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('checklist_model');
}
public function index()
{
$data['check_list'] = $this->checklist_model->get_all_details();
$this->load->view('show_checklist', $data);
}
public function form_dropdown()
{
$this->load->view('insert_checklist');
}
public function data_submitted()
{
$data = array('dropdown_single' => $this->input->post('Technology'));
$this->load->model('checklist_model');
$this->checklist_model->insert_in_db($data);
$this->load->model("checklist_model");
$result = $this->checklist_model->read_from_db($data);
$data['result'] = $result[0];
$this->load->view('insert_checklist', $data);
}
public function add_form()
{
$this->load->view('insert_checklist');
}
public function edit()
{
$id = $this->uri->segment(3);
$data['details'] = $this->checklist_model->getById($id);
$this->load->view('edit', $data);
}
public function delete($id)
{
$this->checklist_model->delete_a_detail($id);
$this->index();
}
public function insert_new_details()
{
$udata['technology_name'] = $this->input->post('technology_name');
$udata['questions'] = $this->input->post('questions');
$udata['status'] = $this->input->post('status');
$udata['comments'] = $this->input->post('comments');
$res = $this->checklist_model->insert_details_to_db($udata);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
public function update()
{
$mdata['technology_name']=$_POST['technology_name'];
$mdata['questions']=$_POST['questions'];
$mdata['status']=$_POST['status'];
$mdata['comments']=$_POST['comments'];
$res=$this->checklist_model->update_info($mdata, $_POST['id']);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
}
?>
it means you call for an array variable's index 'question', and that index does not exist in that array. Maybe in function update(), $_POST['question']. Not sure. You should provide the full error msg, like file and line.
Also: the view if that's the case. And make this sound more like a question.

Load multiple models within same function of controller

I have two functions in my model as
class Jobseeker_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function result_getall($id)
{
$this->db->select('*');
$this->db->from('tbl_jobseeker');
$this->db->where('tbl_jobseeker.User_id',$id);
$this->db->join('tbl_work_exp', 'tbl_jobseeker.User_id = tbl_work_exp.User_id','left');
$query = $this->db->get();
return $query->row();
}
public function select($id)
{
$this->db->select('*');
$this->db->from('tbl_qualification');
$this->db->where('tbl_qualification.User_id',$id);
$query = $this->db->get();
return $query->result();
}
}
And in my controller I have a function as
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$res['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data,$res);
}
It is not possible to display the view page.. I could pass two variables into my view page.right?
You can pass your any number of variables/arrays using a single array.
In Controller:
public function display() {
$id = $this->session->userdata('user_id');
$data['var1'] = $this->jobseeker_model->result_getall($id);
$data['var2'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
In View:
`$var1` and `$var2` will be available.
You can pass your two variable using single srray
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
Views
foreach($a as $data){
// your code
}
echo $row->column_name;
Try this
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}

How can I pass variable between controller functions

This is my first time doing web programming. I want to make one variable that I can use on some functions, I use public $username; and public $password; and use $this->username and $this->password; but it didn't work. This is my code on controller;
public $can_log ;
public function home(){
$this->load->model("model_get");
$data["results"] = $can_log;
$this->load->view("content_home",$data);
}
public function login(){
$this->load->view("site_header");
$this->load->view("content_login");
$this->load->view("site_footer");
}
public function login_validation(){
$this->load->library('form_validation');
$this->load->view("site_header");
$this->load->view("site_nav");
$this->form_validation->set_rules('username','Username','required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|trim');// use md5 if want to encrpyt this
if($this->form_validation->run()){
redirect('site/home');
} else {
$this->load->view('content_login');
}
}
public function validate_credentials(){
$this->load->model('model_get');
$username = $this->input->post('username');//"user";
$password = $this->input->post('password');//"password";
//I tried both but none of those work
$this->can_log = $this->model_get->can_log_in($username, $password);
if($this->can_log){
return true;
} else {
$this->form_validation->set_message('validate_credentials','Incorrect username/password.');
return false;
}
}
I also tried with public $username and public $password but still can't get it
on my model;
public function can_log_in($username, $password){
$query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = '$username' and id_password = '$password'");
if($query->num_rows() > 0) {
$data = $query->result(); // fetches single row: $query->row();
return $data; //fetches single column: $data->col1;
}
}
so how can I get can_log - that contains col1 and col2 - to other function?
Maybe something like this?
public function with_parameter($parameter)
{
do something with $parameter
}
And then call the function
with_parameter($can_log);
I didn't understood the exact requirements, but try below code if it works for you.
Have followed some CI guidelines which you need to learn.
Controller:
class Controller_name extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("model_get"); // load models in constructor
$this->can_log = "some value"; // is the way to define a variable
}
public function home()
{
$data["results"] = $this->can_log; // is the way to retrieve value
$this->load->view("content_home",$data);
}
public function validate_credentials()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$is_valid = $this->model_get->can_log_in($username, $password);
if($is_valid)
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials','Incorrect username/password.');
return false;
}
}
}
Model:
class Model_get extends CI_Model
{
public function can_log_in($username, $password)
{
$where_aray = array("id_login" => $username, "id_password" => $password);
$query = $this->db->get_where("table", $where_array);
if($query->num_rows() > 0)
return $query->row();
return false;
}
}

Categories