1

I've been working to make some CRUD I get nothing wrong. But when I update data I got nothing affected in database and here my code

Table : Song Column : id_song, id_album, song, author, composer

Code for my Model is below:

function Tampil_song_admin(){
 return $this->db->get('songs');
}

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

function edit_song($where,$table){  
 return $this->db->get_where($table,$where);
}

function update_song($where,$data,$table){
 $this->db->where($where);
 $this->db->update($table,$data);
}

function delete_song($where,$table){
 $this->db->where($where);
 $this->db->delete($table);
} 

View:

                 <?php foreach($songs as $data){ ?> 
<form action="<?php echo base_url('/index.php/Admin_song/update_song'); ?>" method="post">

            <h4 class="header-title m-t-0 m-b-30"></h4>

                <div class="form-group">
                    <label for="userName">id_songs </label>
                    <input type="text" name="id_song" parsley-trigger="change" required
                           value="<?php echo $data->id_song; ?>" 
                           class="form-control" disabled="" id="userName">
                </div>                                              
                <div class="form-group">
                    <label for="userName">id_album</label>
                    <input type="text" name="id_album" parsley-trigger="change" required
                           value="<?php echo $data->id_album; ?>"  class="form-control" id="userName">
                </div>

                <div class="form-group">
                    <label for="userName">Songs</label>
                    <input type="text" name="song" parsley-trigger="change" required
                           value="<?php echo $data->song; ?>"  class="form-control" id="userName">
                </div>
                <div class="form-group">
                    <label for="userName">Author</label>
                    <input type="text" name="author" parsley-trigger="change" required
                           value="<?php echo $data->author; ?>"  class="form-control" id="userName">
                </div>
                <div class="form-group">
                    <label for="userName">Composer</label>
                    <input type="text" name="composer" parsley-trigger="change" required
                           value="<?php echo $data->composer; ?>"  class="form-control" id="userName">
                </div>


                <button type="submit" class="btn btn-success waves-effect w-md waves-light m-b-5">Update</button>
                <button type="button" class="btn btn-danger waves-effect w-md waves-light m-b-5">Reset</button>
            </form>

            <?php } ?>

And this is for Controller:

        function index(){
      $data['songs'] = $this->m_admin_data->Tampil_song_admin()->result();
      $this->load->view('admin/v_admin_song',$data);
     }

     //Tambah Data 
      function add_song(){
          $this->load->view('admin/v_admin_song_add');
         }

    function proses_tambah(){
          $id_album = $this->input->post('id_album');
          $song = $this->input->post('song');
          $author = $this->input->post('author');
          $composer = $this->input->post('composer');  

          $data = array(
           'id_album' => $id_album,
           'song' => $song,
           'author' => $author,
           'composer' => $composer
           );
          $this->m_admin_data->Input_song($data,'songs');
          redirect('Admin_song/index');
         } 

    //Update/Edit Data
    function edit_song($id){
      $where = array('id_song' => $id);
      $data['songs'] = $this->m_admin_data->edit_song($where,'songs')->result();
      $this->load->view('admin/v_admin_song_edit',$data);
     }

    function update_song(){
        $id = $this->input->post('id_song');
        $id_album = $this->input->post('id_album');
        $song = $this->input->post('song');
        $author = $this->input->post('author');
        $composer = $this->input->post('composer');  

     $data = array( 
           'id_album' => $id_album,
           'song' => $song, 
           'author' => $author,
           'composer' => $composer

     );

     $where = array(
      'id_song' => $id
     );

     $this->m_admin_data->update_song($where,$data,'songs');
     redirect('Admin_song/index');
    }

    //Hapus Data 
       function delete_song($id){
      $where = array('id_song' => $id);
      $this->m_admin_data->delete_song($where,'songs');
      redirect('Admin_song/index');
     }

See I got nothing wrong but when I edit and try to update some data its not affected.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Riandy Eka
  • 63
  • 8
  • 1
    `print_r($data);die;` in your controller to make sure `post data` is coming from the post form – Pradeep Jul 02 '18 at 05:39
  • you mean in Update Function ? – Riandy Eka Jul 02 '18 at 05:52
  • where else ;p that's where you said your issue was... do it after $data = array(... and before $where = array(... – Alex Jul 02 '18 at 05:56
  • 1
    also you should comment out the redirect for debugging... you might be redirected before you even see an error! don't forget - many people often make it so that values cannot be null in db - without any form validation (which you really should have) a blank submission could cause "item cannot be null" db errors. you can check any database related errors by turning on db_debug in the database.php file – Alex Jul 02 '18 at 06:14
  • yes in `update_song` method before `$where` add this `print_r($data);die;` – Pradeep Jul 02 '18 at 06:23
  • @pradeep can you show how it will be ? I'm a bit confused – Riandy Eka Jul 02 '18 at 07:20
  • Ok do like this `function update_song(){ print_r($this->input->post());die; $id = $this->input->post('id_song'); $id_album = $this->input->post('id_album');.....}` and check what output you r getting after submitting ur form – Pradeep Jul 02 '18 at 07:28
  • @pradeep I get this message : Array ( [id_album] => 1 [song] => Jalan Terus 111 [author] => Papau [composer] => Papau ) I make some change in column "Author" I still dont understand, why after that there's no changes in database – Riandy Eka Jul 02 '18 at 08:18
  • did you get any error ? make sure its using the correct model name and correct column name in where clause? – Pradeep Jul 02 '18 at 08:36
  • @pradeep I'm not sure about the Error cuz after update ting some column record in database not changed – Riandy Eka Jul 02 '18 at 08:59
  • Ok got you - you have just `disabled` your `id_song` text input that is why u r not able to get the value of it which is required for `where` clause , just remove it from there or set it `readonly` or take it into `hidden` field – Pradeep Jul 02 '18 at 09:21
  • For sure :) I'll response – Riandy Eka Jul 03 '18 at 09:00

1 Answers1

1

Hope this will help you :

You have disabled you id_song input type in your form just remove disabled attr from it

Because of this u r unable to get id_song post value in your update_song method in turn not getting in your where clause so

id_song input Should be like this :

 <div class="form-group">
    <label for="userName">id_songs </label>
    <input type="text" 
           name="id_song" 
           value="<?php echo $data->id_song; ?>" 
           class="form-control"  id="userName">
 </div>   

Or just make it hidden if you don't want to show it like this :

    <input type="hidden" 
           name="id_song" 
           parsley-trigger="change" required
           value="<?php echo $data->id_song; ?>" 
           class="form-control"  id="userName">     
Pradeep
  • 9,667
  • 13
  • 27
  • 34