• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't update primary key

#1
Hi everyone, i new in codeigniter and i have problem with my code, i create CRUD aplication with Codeigniter 3 and these my codes:

my table:

Code:
CREATE TABLE `siswa` (
 `nis` varchar(15) NOT NULL,
 `nama` varchar(30) NOT NULL,
 `jkel` char(1) NOT NULL,
 `active` char(1) NOT NULL
ALTER TABLE `siswa`
 ADD PRIMARY KEY (`nis`);
this is my controller:

PHP Code:
<?php

if (!defined('BASEPATH'))
 
   exit('No direct script access allowed');

class 
Siswa extends CI_Controller
{
 
   function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model('Siswa_model');
 
       $this->load->library('form_validation');
 
   }

 
   public function index()
 
   {
 
       $q urldecode($this->input->get('q'TRUE));
 
       $start intval($this->input->get('start'));
 
       
        if 
($q <> '') {
 
           $config['base_url'] = base_url() . 'siswa/index.html?q=' urlencode($q);
 
           $config['first_url'] = base_url() . 'siswa/index.html?q=' urlencode($q);
 
       } else {
 
           $config['base_url'] = base_url() . 'siswa/index.html';
 
           $config['first_url'] = base_url() . 'siswa/index.html';
 
       }

 
       $config['per_page'] = 10;
 
       $config['page_query_string'] = TRUE;
 
       $config['total_rows'] = $this->Siswa_model->total_rows($q);
 
       $siswa $this->Siswa_model->get_limit_data($config['per_page'], $start$q);

 
       $this->load->library('pagination');
 
       $this->pagination->initialize($config);

 
       $data = array(
 
           'siswa_data' => $siswa,
 
           'q' => $q,
 
           'pagination' => $this->pagination->create_links(),
 
           'total_rows' => $config['total_rows'],
 
           'start' => $start,
 
       );
 
       $this->load->view('siswa/siswa_list'$data);
 
   }

 
   public function read($id
 
   {
 
       $row $this->Siswa_model->get_by_id($id);
 
       if ($row) {
 
           $data = array(
        
'nis' => $row->nis,
        
'nama' => $row->nama,
        
'jkel' => $row->jkel,
        
'active' => $row->active,
     
   );
 
           $this->load->view('siswa/siswa_read'$data);
 
       } else {
 
           $this->session->set_flashdata('message''Record Not Found');
 
           redirect(site_url('siswa'));
 
       }
 
   }

 
   public function create() 
 
   {
 
       $data = array(
 
           'button' => 'Create',
 
           'action' => site_url('siswa/create_action'),
     
   'nis' => set_value('nis'),
     
   'nama' => set_value('nama'),
     
   'jkel' => set_value('jkel'),
     
   'active' => set_value('active')
    );
 
       $this->load->view('siswa/siswa_form'$data);
 
   }
 
   
    public 
function create_action() 
 
   {
 
       $this->_rules();

 
       if ($this->form_validation->run() == FALSE) {
 
           $this->create();
 
       } else {
 
           $data = array(
        
'nis' => $this->input->post('nis',TRUE),
        
'nama' => $this->input->post('nama',TRUE),
        
'jkel' => $this->input->post('jkel',TRUE),
        
'active' => $this->input->post('active',TRUE),
     
   );

 
           $this->Siswa_model->insert($data);
 
           $this->session->set_flashdata('message''Create Record Success');
 
           redirect(site_url('siswa'));
 
       }
 
   }
 
   
    public 
function update($id
 
   {
 
       $row $this->Siswa_model->get_by_id($id);

 
       if ($row) {
 
           $data = array(
 
               'button' => 'Update',
 
               'action' => site_url('siswa/update_action'),
        
'nis' => set_value('nis'$row->nis),
        
'nama' => set_value('nama'$row->nama),
        
'jkel' => set_value('jkel'$row->jkel),
        
'active' => set_value('active'$row->active),
     
   );
 
           $this->load->view('siswa/siswa_form'$data);
 
       } else {
 
           $this->session->set_flashdata('message''Record Not Found');
 
           redirect(site_url('siswa'));
 
       }
 
   }
 
   
    public 
function update_action() 
 
   {
 
       $this->_rules();

 
       if ($this->form_validation->run() == FALSE) {
 
           $this->update($this->input->post('nis'TRUE));
            
 
       } else {
 
           $data = array(
        
'nis' => $this->input->post('nis',TRUE),
        
'nama' => $this->input->post('nama',TRUE),
        
'jkel' => $this->input->post('jkel',TRUE),
        
'active' => $this->input->post('active',TRUE),
     
   );

 
           $this->Siswa_model->update($this->input->post('nis'TRUE), $data);
 
           $this->session->set_flashdata('message''Update Record Success');
 
           redirect(site_url('siswa'));
 
       }
 
   }
 
   
    public 
function delete($id
 
   {
 
       $row $this->Siswa_model->get_by_id($id);

 
       if ($row) {
 
           $this->Siswa_model->delete($id);
 
           $this->session->set_flashdata('message''Delete Record Success');
 
           redirect(site_url('siswa'));
 
       } else {
 
           $this->session->set_flashdata('message''Record Not Found');
 
           redirect(site_url('siswa'));
 
       }
 
   }

 
   public function _rules() 
 
   {
    
$this->form_validation->set_rules('nis''nis''trim|required');
    
$this->form_validation->set_rules('nama''nama''trim|required');
    
$this->form_validation->set_rules('jkel''jkel''trim|required');
    
$this->form_validation->set_rules('active''active''trim|required');

    
//$this->form_validation->set_rules('nis', 'nis', 'trim');
    
$this->form_validation->set_error_delimiters('<span class="text-danger">''</span>');
 
   }



this is my model:
PHP Code:
<?php

if (!defined('BASEPATH'))
 
   exit('No direct script access allowed');

class 
Siswa_model extends CI_Model
{

 
   public $table 'siswa';
 
   public $id 'nis';
 
   public $order 'DESC';

 
   function __construct()
 
   {
 
       parent::__construct();
 
   }

 
   // get all
 
   function get_all()
 
   {
 
       $this->db->order_by($this->id$this->order);
 
       return $this->db->get($this->table)->result();
 
   }

 
   // get data by id
 
   function get_by_id($id)
 
   {
 
       $this->db->where($this->id$id);
 
       return $this->db->get($this->table)->row();
 
   }
 
   
    
// get total rows
 
   function total_rows($q NULL) {
 
       $this->db->like('nis'$q);
    
$this->db->or_like('nama'$q);
    
$this->db->or_like('jkel'$q);
    
$this->db->or_like('active'$q);
    
$this->db->from($this->table);
 
       return $this->db->count_all_results();
 
   }

 
   // get data with limit and search
 
   function get_limit_data($limit$start 0$q NULL) {
 
       $this->db->order_by($this->id$this->order);
 
       $this->db->like('nis'$q);
    
$this->db->or_like('nama'$q);
    
$this->db->or_like('jkel'$q);
    
$this->db->or_like('active'$q);
    
$this->db->limit($limit$start);
 
       return $this->db->get($this->table)->result();
 
   }

 
   // insert data
 
   function insert($data)
 
   {
 
       $this->db->insert($this->table$data);
 
   }

 
   // update data
 
   function update($id$data)
 
   {    
    
$this->db->update($this->table$data);
 
   }

 
   // delete data
 
   function delete($id)
 
   {
 
       $this->db->where($this->id$id);
 
       $this->db->delete($this->table);
 
   }




this is my view:
siswa_form

Code:
<!doctype html>
<html>
   <head>
       <title>Siswa Form</title>
       <link rel="stylesheet" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>"/>
       <style>
           body{
               padding: 15px;
           }
       </style>
   </head>
   <body>
       <h2 style="margin-top:0px">Siswa <?php echo $button ?></h2>
       <form action="<?php echo $action; ?>" method="post">
        <div class="form-group">
           <label for="varchar">NIS <?php echo form_error('nis') ?></label>
           <input type="text" class="form-control" name="nis" id="nis" placeholder="Nis" value="<?php echo $nis; ?>" />
       </div>
        <div class="form-group">
           <label for="varchar">Nama <?php echo form_error('nama') ?></label>
           <input type="text" class="form-control" name="nama" id="nama" placeholder="Nama" value="<?php echo $nama; ?>" />
       </div>
        <div class="form-group">
           <label for="char">Jkel <?php echo form_error('jkel') ?></label>
           <input type="text" class="form-control" name="jkel" id="jkel" placeholder="Jkel" value="<?php echo $jkel; ?>" />
       </div>
        <div class="form-group">
           <label for="char">Active <?php echo form_error('active') ?></label>
           <input type="text" class="form-control" name="active" id="active" placeholder="Active" value="<?php echo $active; ?>" />
       </div>
        <!-- <input type="hidden" name="nis" value="<?php// echo $nis; ?>" /> -->
        <button type="submit" class="btn btn-primary"><?php echo $button ?></button>
        <a href="<?php echo site_url('siswa') ?>" class="btn btn-default">Cancel</a>
    </form>
   </body>
</html>

siswa_list
Code:
<!doctype html>
<html>
   <head>
       <title>Aditya BN</title>
       <link rel="stylesheet" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>"/>
       <style>
           body{
               padding: 15px;
           }
       </style>
   </head>
   <body>
       <h2 style="margin-top:0px">Siswa List</h2>
       <div class="row" style="margin-bottom: 10px">
           <div class="col-md-4">
               <?php echo anchor(site_url('siswa/create'),'Create', 'class="btn btn-primary"'); ?>
           </div>
           <div class="col-md-4 text-center">
               <div style="margin-top: 8px" id="message">
                   <?php echo $this->session->userdata('message') <> '' ? $this->session->userdata('message') : ''; ?>
               </div>
           </div>
           <div class="col-md-1 text-right">
           </div>
           <div class="col-md-3 text-right">
               <form action="<?php echo site_url('siswa/index'); ?>" class="form-inline" method="get">
                   <div class="input-group">
                       <input type="text" class="form-control" name="q" value="<?php echo $q; ?>">
                       <span class="input-group-btn">
                           <?php
                               if ($q <> '')
                               {
                                   ?>
                                   <a href="<?php echo site_url('siswa'); ?>" class="btn btn-default">Reset</a>
                                   <?php
                               }
                           ?>
                         <button class="btn btn-primary" type="submit">Search</button>
                       </span>
                   </div>
               </form>
           </div>
       </div>
       <table class="table table-bordered" style="margin-bottom: 10px">
           <tr>
               <th>No</th>
        <th>NIS</th>    
        <th>Nama</th>
        <th>Jkel</th>
        <th>Active</th>
        <th>Action</th>
           </tr><?php
           foreach ($siswa_data as $siswa)
           {
               ?>
               <tr>
            <td width="80px"><?php echo ++$start ?></td>
            <td><?php echo $siswa->nis ?></td>
            <td><?php echo $siswa->nama ?></td>
            <td><?php echo $siswa->jkel ?></td>
            <td><?php echo $siswa->active ?></td>
            <td style="text-align:center" width="200px">
                <?php
                echo anchor(site_url('siswa/read/'.$siswa->nis),'Read');
                echo ' | ';
                echo anchor(site_url('siswa/update/'.$siswa->nis),'Update');
                echo ' | ';
                echo anchor(site_url('siswa/delete/'.$siswa->nis),'Delete','onclick="javasciprt: return confirm(\'Are You Sure ?\')"');
                ?>
            </td>
        </tr>
               <?php
           }
           ?>
       </table>
       <div class="row">
           <div class="col-md-6">
               <a href="#" class="btn btn-primary">Total Record : <?php echo $total_rows ?></a>
        </div>
           <div class="col-md-6 text-right">
               <?php echo $pagination ?>
           </div>
       </div>
   </body>
</html>
my problem is , i want to update nis data where nis is primary key, i always failed to update it, how i can update this nis ?

thank you,
Best regards.
Reply

#2
here in your model - your update method

PHP Code:
// update data
    
function update($id$data)
    {    
    
$this->db->update($this->table$data);
    } 

you are passing $id and $data to update method...
but then you aren't using the $id
so then the database does not know which record you want to update

so you want a line in the method before the update line, like
PHP Code:
$this->db->where('nis'$id); 
Reply

#3
i follow your suggest and this my model:

PHP Code:
// update data
 
   function update($id$data)
 
   {    
        
$this->db->where('nis'$id);
        
$this->db->update($this->table$data);
 
   

or i try this too:
PHP Code:
// update data
 
   function update($id$data)
 
   {    
        
$this->db->where($this->id$id);
        
$this->db->update($this->table$data);
 
   

it's still not working.
Reply

#4
(11-08-2016, 05:28 PM)uglyaditya Wrote: i follow your suggest and this my model:

PHP Code:
// update data
 
   function update($id$data)
 
   {    
        
$this->db->where('nis'$id);
        
$this->db->update($this->table$data);
 
   

or i try this too:
PHP Code:
// update data
 
   function update($id$data)
 
   {    
        
$this->db->where($this->id$id);
        
$this->db->update($this->table$data);
 
   

it's still not working.


Hi! As for update goes, you can update the rest of the columns by keeping the primary key as anchor.  But in your case you are trying to update primary key too ('nis').

Change your controller's update_action() function like this,

PHP Code:
public function update_action()
{
 
   ...
 
   
    
} else {
 
       $data = array(
 
       'nama' => $this->input->post('nama',TRUE),
 
       'jkel' => $this->input->post('jkel',TRUE),
 
       'active' => $this->input->post('active',TRUE),
 
       );

 
   ...
 
   }


And what @cartalot suggested is also true.  You have to use where clause in model's update() function to let the db know what record should be updated.

Your model fn should be this,
PHP Code:
function update($id$data)
   
    $this
->db->where('nis'$id);
 
   $this->db->update($this->table$data);


Hope this helps Smile
Reply

#5
hi  KimZ,
thanks for your reply, so i must use if else in my controller?
can you give me full code for controller for update's function?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.