Error Number: 1064 in deleting data using Codeigniter 3 - php

I want to deleting my data in the table, but i can't delete the data because of this problem. Error Number: 1064
This is my Controller
class Dosen extends CI_Controller
{
public function __construct()
{
parent ::__construct();
$this->load->library('form_validation');
$this->load->model('Dosen_model');
}
public function index()
{
$data['judul'] = 'Daftar Dosen';
$data['dosen'] = $this->Dosen_model->getAllDosen();
$this->load->view('templates/header', $data);
$this->load->view('dosen/index', $data);
$this->load->view('templates/footer');
}
public function tambah()
{
$data['judul'] = 'Form tambah data Dosen';
$this->form_validation->set_rules('nip','NIP','required');
$this->form_validation->set_rules('nama','Nama','required');
if ($this->form_validation->run()==FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('dosen/tambah');
$this->load->view('templates/footer');
}
else
{
$this->Dosen_model->tambahDataDosen();
$this->session->set_flashdata('flash','Ditambahkan');
redirect('Dosen');
}
}
public function hapus($nip)
{
$this->Dosen_model->hapusDataDosen($nip);
$this->session->set_flashdata('flash','Dihapus');
redirect('Dosen');
}
And this is my Model
<?php
class Dosen_model extends CI_Model
{
public function getAllDosen()
{
$query = $this->db->get('daftar_dosen');
return $query->result_array();
}
public function tambahDataDosen()
{
$data = [
"nip" => $this->input->post('nip'),
"nama" => $this->input->post('nama'),
"prodi" => $this->input->post('prodi'),
];
$this->db->insert('daftar_dosen',$data);
}
public function hapusDataDosen($nip)
{
$this->db->where('nip',$nip);
$this->db->delete('daftar_dosen',$data);
}
}
the output is :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: models/Dosen_model.php
Line Number: 23
Backtrace:
File: C:\xampp\htdocs\endtest\application\models\Dosen_model.php
Line: 23
Function: _error_handler
File: C:\xampp\htdocs\endtest\application\controllers\Dosen.php
Line: 40
Function: hapusDataDosen
File: C:\xampp\htdocs\endtest\index.php
Line: 315
Function: require_once
and then
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IS NULL' at line 3
DELETE FROM daftar_dosen WHERE nip = '0001' AND IS NULL
Filename: C:/xampp/htdocs/endtest/system/database/DB_driver.php
Line Number: 691

The error message is very clear
Message: Undefined variable: data
In other words, somewhere there is a variable $data that is not defined.
Where in your code is $data found? The answer: In the function hapusDataDosen() in the call to $this->db->delete('daftar_dosen',$data);
It is hard to know exactly what you are trying to do. My guess is all you need to do is change
$this->db->delete('daftar_dosen', $data);
to
$this->db->delete('daftar_dosen');

Try this
public function hapusDataDosen($nip){
$this->db->where('nip',$nip);
$this->db->delete('daftar_dosen');
}
OR
public function hapusDataDosen($nip){
$this->db->delete('daftar_dosen', ['nip' => $nip]);
}
Don't use $data because
You are not passing it to model function
It is unnecessary to delete a record from DB

Try This --
In Model
public function hapusDataDosen($nip)
{
$this->db->where('nip',$nip);
$this->db->delete('daftar_dosen',$nip);
}

public function hapusDataDosen($nip){
$this->db->delete('daftar_dosen',array('nip'=>$nip));
}

Related

Call to undefined method in Codeigniter 3

So i just want to make simple form to add, update, and delete some data(Name, id, and address). Add and delete works fine for me, but the update doesn't, codeigniter give me a error message :
A PHP Error was encountered
Severity: Error
Message: Call to undefined method Mahasiswa_model::getMahasiswa()
Filename: controllers/Mahasiswa.php
Line Number: 35
Backtrace:
And these is my code which i thought its related with the error.
This one file name is Mahasiswa.php
<?php
if (!defined('BASEPATH')) exit('no direcet script access allowed');
class Mahasiswa extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Mahasiswa_model');
}
public function index() {
$data['mhs'] = $this->Mahasiswa_model->retrieve();
$this->load->view('Mahasiswa_view', $data);
}
public function form_tambah() {
$this->load->view('Tambah_view');
}
public function submit() {
$this->Mahasiswa_model->add($this->input->post('var'));
$data['submitted'] = TRUE;
$this->load->view('Tambah_view', $data);
}
function delete() {
$this->Mahasiswa_model->delete($this->uri->rsegment(3));
$this->index();
}
function form_update() {
$data['mhs'] = $this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
$this->load->view('update_view', $data);
}
function update() {
$this->Mahasiswa_model->update($this->input->post('old_nim'),
$this->input->post('var'));
$this->index();
}
}
?>
And this one is Mahasiswa_model.php
<?php
class Mahasiswa_model extends CI_Model {
function retrieve() {
$query = $this->db->get('mhs');
if($query->result()) {
foreach ($query->result() as $content) {
$data[] = array(
$content->nim,
$content->nama,
$content->alamat
);
}
return $data;
} else {
return FALSE;
}
}
function add($arg) {
$data = array (
'nim' => $arg[0],
'nama'=> $arg[1],
'alamat' => $arg[2],
);
$this->db->insert('mhs', $data);
}
function delete($id) {
$this->db->where('nim', $id);
$this->db->delete('mhs');
}
function update($id, $form) {
$data = array(
'nim' => $form[0],
'nama' =>$form[1],
'alamat' => $form[2],
);
}
}
?>
Maybe someone can help me find where mistake i made? Line 35 not clear enough for me
This line in your controller is the problem:
$this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
You're calling the getMahasiswa method that should be available in your model, but (assuming the code you posted for your model is complete) it's not. Your model only has the retrieve, add, delete and update methods declared.
you either need to create the getMahasiswa method in your model, or you need to call a different method from your controller.
if you want to call a function from same controller then simply write.
$data = $this->getMahasiswa($parameter1);
IMP : function must be present there in the same controller.

Error : Undefined property: Account::$Account_model

A PHP Error was encountered Severity: Notice
Message: Undefined property: Account::$Account_model
Filename: Admin/Account.php
Line Number: 19
Backtrace:
File: C:\xampp\htdocs\AdminLTE\application\controllers\Admin\Account.php
Line: 19
Function: _error_handler
File: C:\xampp\htdocs\AdminLTE\index.php
Line: 292
Function: require_once
class Account extends CI_Controller {
public function _construct() {
parent::__construct();
$this->load->model('Account_model');
}
public function index() {
$mdat=[
'active_controller' => 'master',
'active_function' => 'account/account_view',
];
$this->load->view('admin/global/menu', $mdat);
$data['books']=$this->Account_model->get_all_acc();
$this->load->view('account_view',$data);
}
this is my model :
class Account_model extends CI_Model
{
var $table = 'books';
public function acc_add($data) {
$this->db->insert($this->table,$data);
return $this->db->insert_id();
}
public function get_all_acc() {
$this->db->from('books');
$query = $this->db->get();
return $query->result();
}
Try to change your function construct to this one :
public function __construct() {
parent::__construct();
$this->load->model('Account_model');
}
because the construct function require double underscore and you just typing one underscore

A PHP Error was encountered Severity: Notice Message: Undefined variable: result

A PHP Error was encountered Severity: Notice
Message: Undefined variable: result
Filename: user/messages.php
Line Number: 87
Backtrace:
File: E:\xampp\htdocs\ci\application\views\user\messages.php Line: 87
Function: _error_handler
File: E:\xampp\htdocs\ci\application\controllers\users\Dashboard.php
Line: 12 Function: view
File: E:\xampp\htdocs\ci\index.php Line: 315 Function: require_once
here is my code
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Bills extends CI_Controller{
function __construct(){
parent::__construct();
}
public function bill(){
$id = $this->session->userdata('email');
$this->load->database();
$this->load->helper(array('form', 'url', 'html'));
$this->load->model('fuser/Bill');
$data['result'] = $this->Bill->bills();
$this->load->view('user/messages',$data);
}
}
?>
**Model**
<?php
class Bill extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//we will use the select function
public function bills($id)
{
//data is retrive from this query
$query = $this->db->get('invoices');
return $query->result();
}
}
?>
**View**
<div class="billing_tabs_text_part">
<?php foreach($result as $row){?>
<p><?php echo $row['book_id']; }?></p>
</div>
i am not sure which section is your problem so i suggest something how to find error
public function bill()
{
echo 'ok'; // check controller work
$id = $this->session->userdata('email');
$this->load->database();
echo 'ok1';
$this->load->helper(array('form', 'url', 'html'));
echo 'ok2';
/*$this->load->model('fuser/Bill');
$data['result'] = $this->Bill->bills();
$this->load->view('user/messages', $data);*/
}
if this is print ok, ok1 , ok2 then this is query problem , if no print ok,ok1 then this is not query problem
in this case try to find error, if other ok then must print ok
public function bill()
{
echo 'ok';
/*$id = $this->session->userdata('email');
$this->load->database();
$this->load->helper(array('form', 'url', 'html'));
$this->load->model('fuser/Bill');
$data['result'] = $this->Bill->bills();
$this->load->view('user/messages', $data);*/
}
use <?php echo $row->book_id; }?> in view
because your data is object array.
You need to find where the problem is exactly
try to var_dump the results and see if you're actually getting any results and that result is not a null or false;
so before this line:
$data['result'] = $this->Bill->bills();
try to
var_dump($this->Bill->bills());
die();
if you're getting results try the other way using dummy content instead of $this->Bill->bills(); use :
$data['result'] = [0 => 1, 1 =>2]; and var_dump from the view
see if you're getting results, and in line where you have
return $query->result();
make it like this:
return $query->result() ? : [];
What gives you $query->result(); try to print in model itself
don't return , print and check the result
ex:
var_dump($query->result());

Fix Error Codeigniter : Call to a member function pilihkat() on a non-object

Error :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysqli_result::$Input_model
Filename: controllers/Input_App.php
Line Number: 18
Backtrace:
File: C:\xampp\htdocs\admin_app\application\controllers\Input_App.php
Line: 18
Function: _error_handler
File: C:\xampp\htdocs\admin_app\index.php
Line: 315
Function: require_once
and
Fatal error: Call to a member function pilihkat() on a non-object in C:\xampp\htdocs\admin_app\application\controllers\Input_App.php on line 18
My controller
class Input_App extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Input_model','input_app');
}
public function index()
{
$this->db->from('referensi_kategori');
$query = $this->db->get();
$load['pilihkategori'] = $query->Input_model->pilihkat()->result();
$this->load->view('input_view',$load);
}
Model (pilihkat())
public function pilihkat(){
$this->db->select('*');
$this->db->join('referensi_kategori','referensi_kategori.id_kategori = data_aplikasi.id_kategori','left');
$query = $this->db->get();
return $query->result();
}
View (Select dropdown from data_kategori)
<tr>
<td>Kategori</td>
<td><select name="data_kategori" class="form-control"required id="data_kategori" style="width:85%;">
<option value='0'>-- pilih kategori --</option>
<?php foreach ($pilihkategori as $kat) {
echo "<option value=".$kat->id_kategori.">".$kat->data_kategori."</option>";
}?>
</select></td>
</tr>
How to fix this error ?
Load the model like this
$this->load->model('Input_model','input_model'); //here input_model is alias name
$this->load->model('Input_app','input_app'); //here input_app is alias name
(or)
$this->load->model(array('Input_model','input_app')); // pass as array mutiple modal .in below i'm using this one
remove result() in controller while calling model because in model your returning result() so in controller no need to call it again.
$load['pilihkategori'] = Input_model->pilihkat();
Controller :
public function index()
{
$load['pilihkategori'] = Input_model->pilihkat();
$this->load->view('input_view',$load);
}
Model :
public function pilihkat()
{
$this->db->select('*');
$this->db->from('referensi_kategori');
$this->db->join('referensi_kategori','referensi_kategori.id_kategori = data_aplikasi.id_kategori','left');
$query = $this->db->get();
return $query->result();
}
In your controller need to change two things.
1.Load more than one models like this.
$this->load->model(array('Input_model','input_app'));
2.Remove result() Because you already done it in model.
public function index()
{
$load['pilihkategori'] = $query->Input_model->pilihkat();
$this->load->view('input_view',$load);
}

codeigniter news section tutorial not working

hey guys im going thru the news section tutorial on codeigniter and i get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$db
Filename: core/Model.php
Line Number: 77
Backtrace:
File: /Applications/MAMP/htdocs/CodeIgniter/application/models/News_model.php
Line: 13
Function: __get
File: /Applications/MAMP/htdocs/CodeIgniter/application/controllers/News.php
Line: 10
Function: get_news
File: /Applications/MAMP/htdocs/CodeIgniter/index.php
Line: 292
Function: require_once
All i did was simply copy what was provided in the tutorial, here are my files
news_model.php file
<?php
class News_model extends CI_Model {
public function __constrcut() {
$this->load->database();
}
public function get_news($slug = FALSE) {
if ($slug === FALSE ) {
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
news.php
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
}
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
public function view($slug) {
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news_item'])) {
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header',$data);
$this->load->view('news/view',$data);
$this->load->view('templates/footer');
}
}
and here is a picture of my file structure
im trying my best to learn and debug but i dont know what it could be.
Maybe, database library is not loaded. you have to load database library.
application / config / autoload.php
$autoload['libraries'] = array('database');

Categories