我目前正在开发一个使用php jquery和bootstrap的程序,更新函数,有一个错误,这样数据库不会改变,但脚本运行正常,也许有人能够帮助我,我的错误在哪里?谢谢
我的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Subbidang extends CI_Controller {
public function index()
{
$this->load->model('model_subbidang');
$this->model_security->getsecurity();
$isi['content'] = 'Subbidang/tampilan_datasubbidang';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Sub Bidang';
$isi['data'] = $this->db->get('tsubbidang');
$this->load->view('tampilan_home',$isi);
}
public function tambah()
{
$this->load->model('model_subbidang');
$this->model_security->getsecurity();
$isi['content'] = 'Subbidang/form_tambahsubbidang';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Sub Bidang';
$this->load->view('tampilan_home',$isi);
}
public function simpan()
{
$this->model_security->getsecurity();
$this->load->model('model_subbidang');
$key['IDSubBidang'] = $this->input->post('kode');
$data['IDSubBidang'] = $this->input->post('kode');
$data['IDBidang'] = $this->input->post('bidang');
$data['NamaSubBidang'] = $this->input->post('subbidang');
$query = $this->db->get_where('tsubbidang',$key);
if ($query->num_rows()>0)
{
$this->db->update('tsubbidang',$key,$data);
//echo "Data Berhasil Diubah";
echo "Update";
}
else
{
$this->db->insert('tsubbidang',$data);
//xecho "Data Berhasil Disimpan";
echo 'Insert';
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
我的观点
<script type="text/javascript">
$(document).ready(function(){
$("#simpan").click(function(){
var string = $("#my-form").serialize();
//alert(string);
$.ajax({
type : 'POST',
url : '<?php echo site_url();?>/subbidang/simpan',
data : string,
success : function(data){
alert(data);
}
});
});
});
</script>
<form class="form-horizontal" name="my-form" id="my-form">
<div class="control-group">
<label class="control-label"bidang</label>
<div class="controls">
<select name="bidang" id="bidang">
<option value="">-Pilih-</option>
<?php
$bidang = $this->db->get('tbidang');
foreach ($bidang->result() as $row) {
?>
<option value="<?php echo $row->IDBidang;?>"><?php echo $row->Bidang;?> </option>
<?php };?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Kode Sub Bidang</label>
<div class="controls">
<input type="text" name="kode" id="kode" palceholder="kode" class="span3">
</div>
</div>
<div class="control-group">
<label class="control-label">Sub Bidang</label>
<div class="controls">
<input type="text" name="subbidang" id="subbidang" palceholder="Sub Bidang" class="span3">
</div>
</div>
<div class="controls">
<button type="button" name="simpan" id="simpan" class="btn btn-primary btn-small"> Simpan</button>
</div>
我的模特
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_subbidang extends CI_model {
public function gettampildatasubbidang($key)
{
$this->db->where('IDBidang',$key);
$query = $this->db->get('tbidang');
if($query->num_rows()>0)
{
foreach ($query -> result() as $row) {
$hasil = $row->Bidang;
}
}
else
{
$hasi='';
}
return $hasil;
}
public function getlistsubbidang()
{
return $this->db->get('tbidang');
}
public function getdata($key)
{
$this->db->where('IDBidang',$key);
$hasil = $this->db->get('tBidang');
return $hasil;
}
public function getupdate($key,$data)
{
$this->db->where('IDBidang',$key);
$this->db->update('tBidang',$data);
}
public function getinsert($key,$data)
{
$this->db->insert('tBidang',$data);
}A
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
echo工作正常但数据库没有变化。 Insert函数也可以正常工作
答案 0 :(得分:0)
您需要将正确的数据传递给更新功能 变化
$this->db->update('tsubbidang',$key,$data);
到
$this->db->where('IDBidang', $key);
$this->db->update('tsubbidang',$data);
一切都会好的。 还有一件事是你必须在模型中不直接在控制器中执行与数据库相关的功能,而不是根据MVC结构。它毁了它。如果你完成了所有事情,那么很容易理解并清楚每个在你之后编码的人。