Crud in Codeigniter not affected when Update to database

Multi tool use
Multi tool use


Crud in Codeigniter not affected when Update to database



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>


id_songs
id_song; ?>"
class="form-control" disabled="" id="userName">


id_album
id_album; ?>" class="form-control" id="userName">



Songs
song; ?>" class="form-control" id="userName">


Author
author; ?>" class="form-control" id="userName">


Composer
composer; ?>" class="form-control" id="userName">



<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.





print_r($data);die; in your controller to make sure post data is coming from the post form
– pradeep
Jul 2 at 5:39


print_r($data);die;


post data





you mean in Update Function ?
– Riandy Eka
Jul 2 at 5:52





where else ;p that's where you said your issue was... do it after $data = array(... and before $where = array(...
– Alex
Jul 2 at 5:56





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 2 at 6:14





yes in update_song method before $where add this print_r($data);die;
– pradeep
Jul 2 at 6:23


update_song


$where


print_r($data);die;




1 Answer
1



Hope this will help you :



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


id_song


form


disabled



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


update_song


where



id_song input Should be like this :


id_song



id_songs
id_song; ?>"
class="form-control" id="userName">



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">





Yes its work and now my CRUD working properly :) , and now if you dont mind to upvote my thread so another people can learn.
– Riandy Eka
Jul 3 at 9:00






glad to hear, happy coding
– pradeep
Jul 3 at 9:03





Yess thanks once again and dont forget to upvote my thread hehehe
– Riandy Eka
Jul 3 at 9:03





do upvote my answer too
– pradeep
Jul 3 at 9:05





Yess, after i get minimum reputation 15 :)
– Riandy Eka
Jul 3 at 9:09






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

DSL,Q s7ZwuNHNf,2Ln,73dEB bAK5FGeQGy7mGF2J,9gtGu1AWKS u f,H,U 50O
Gv WjyYQ8,WJlf OizZ0COV,6

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications