代码点火器中的 crud 在更新到数据库时不受影响



我一直在努力制作一些 CRUD,我没有做错任何事。 但是当我更新数据时,我在数据库中没有任何影响,这是我的代码

表 :歌曲列:id_song, id_album, 歌 作者 作曲家

我的模型的代码如下:

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);
} 

视图:

<?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 } ?>

这是针对控制器的:

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');
}

看到我没有做错任何事,但是当我编辑并尝试更新某些数据时,它不受影响。

希望这会对您有所帮助:

您已禁用id_songform输入类型,只需从中删除disabled属性即可

因此,您无法在update_song方法中获得id_song值,反过来也不会获得where子句,因此

id_song输入应该是这样的:

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

或者,如果您不想像这样显示它,请将其隐藏:

<input type="hidden" 
name="id_song" 
parsley-trigger="change" required
value="<?php echo $data->id_song; ?>" 
class="form-control"  id="userName">     

相关内容

  • 没有找到相关文章

最新更新