(ASK)使用codeIgniter向数据库mysqli添加和更新数据失败

时间:2015-12-14 14:11:11

标签: php database codeigniter mysqli

所以我试着做一个简单的crud应用程序。我尝试删除时效果很好,否则在添加和更新数据时总会出错。我的脚本没有成功添加或更新数据到数据库。我在这里使用代码点火器php框架

以下是我的控制器脚本和参考模型

    <?php
    //script of controller
    class Siswa extends CI_Controller
    {
      private $limit=10;

      function __construct()
      {
        parent::__construct();
        $this->load->library(array('table','form_validation'));
        $this->load->helper(array('form','url'));
        $this->load->model('siswa_model','',TRUE);
      }

      function index($offset=0,$order_column='id',$order_type='asc')
      {
        if(empty($offset))
            $offset=0;

        if(empty($order_column))
            $order_column='id';

        if(empty($order_type))
            $order_type='asc';

        $siswas=$this->siswa_model->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();

        $this->load->library('pagination');
        $config['base_url'] = site_url('siswa/index');
        $config['total_rows'] = $this->siswa_model->count_all();
        $config['per_page']=$this->limit;
        $config['url_segment']=3;
        $this->pagination->initialize($config);
        $data['pagination']=$this->pagination->create_links();

        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $new_order=($order_type=='asc'?'desc':'asc');
        $this->table->set_heading
        (
            'No',
            anchor('siswa/index/'.$offset.'/nama'.$new_order,'Nama'),
            anchor('siswa/index/'.$offset.'/alamat'.$new_order,'Alamat'),
            anchor('siswa/index'.$offset.'/jenis_kelamin'.$new_order,'Jenis Kelamin'),
            anchor('siswa/index'.$offset.'/tanggal_lahir'.$new_order,'Tanggal Lahir (dd-mm-yyyy)'),
            'Actions'
        );
        $i=0+$offset;
        foreach ($siswas as $siswa)
        {
            $this->table->add_row(++$i,
                $siswa->nama,
                $siswa->alamat,
                strtoupper($siswa->jenis_kelamin)=='M'?
                'Laki-laki':'Perempuan',
                date('d-m-Y',strtotime(
                    $siswa->tanggal_lahir)),
                anchor('siswa/view/'.$siswa->id,
                    'view',array('class'=>'view')).' '.
                anchor('siswa/update/'.$siswa->id,
                    'update',array('class'=>'update')).' '.
                anchor('siswa/delete/'.$siswa->id,
                    'delete',array('class'=>'delete',
                        'onclick'=>"return confirm
                        ('Apakah Anda yakin ingin menghapus data siswa?')"))
                );
        }

        $data['table']=$this->table->generate();

        if($this->uri->segment(3)=='delete_success')
            $data['message']='Data berhasil dihapus';

        else if ($this->uri->segment(3)=='add_succsess')
            $data['message']='Data berhasil ditambah';

        else
            $data['message']='';

        $this->load->view('siswaList',$data);
    }

    function add()
    {
        $data['title']='Tambah siswa baru';
        $data['action']= site_url('siswa/add');
        $data['link_back'] = anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));

        $this->_set_rules();
        if($this->form_validation->run() === FALSE)
        {
            $data['message']='';
            $data['title']='Add new siswa';
            $data['message'] = '';
            $data['siswa']['id']='';
            $data['siswa']['nama']='';
            $data['siswa']['alamat']='';
            $data['siswa']['jenis_kelamin']='';
            $data['siswa']['tanggal_lahir']='';
            $data['link_back'] = anchor('siswa/index','Lihat Daftar Siswa',array('class'=>'back'));

            $this->load->view('siswaEdit',$data);
        }

        else
        {
            $siswa = array('nama'=>$this->input->post('nama'),
                'alamat'=>$this->input->post('alamat'),
                'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
                'tanggal_lahir'=>date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));

            $this->validation->id = $id;
            $data['siswa']=$this->siswa_model->add($id,$siswa);
            redirect('siswa/index/add_success');
        }   

    }

    function view($id)
    {
        $data['title']='siswa Details';
        $data['link_back']= anchor('siswa/index/','Lihat daftar siswas',array('class'=>'back'));

        $data['siswa']=$this->siswa_model->get_by_id($id)->row();

        $this->load->view('siswaView',$data);
    }

    function update($id)
    {
        $data['title']='Update siswa';
        $this->load->library('form_validation');
        $this->_set_rules();
        $data['action']=('siswa/update/'.$id);

        if($this->form_validation->run()===FALSE)
        {
            $data['message']='';
            $data['siswa']=$this->siswa_model->get_by_id($id)->row_array();
            $_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
            $data['siswa']['tanggal_lahir']=date('d-m-Y',strtotime($data['siswa']['tanggal_lahir']));   

            $data['title']='Update siswa';
            $data['message']='';    
        }

        else
        {
            $id=$this->input->post('id');
            $siswa=array('nama'=>$this->input->post('nama'),
                'alamat'=>$this->input->post('alamat'),
                'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
                'tanggal_lahir'=> date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));

            $this->siswa_model->update($id,$siswa);

            $data['message']='update siswa success';
        }

        $data['link_back']= anchor('siswa/index/','Lihat daftar siswas',array('class'=>'back'));

        $this->load->view('siswaEdit',$data);
    }

    function delete($id)
    {
        $this->siswa_model->delete($id);
        redirect('siswa/index/delete_success','refresh');

    }       

    function _set_rules()
    {
        $this->form_validation->set_rules('nama','Nama','required|trim');
        $this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required');
        $this->form_validation->set_rules('alamat','Alamat','required');
        $this->form_validation->set_rules('tanggal_lahir','Tanggal Lahir','required|callback_valid_date');
    }

    function valid_date($str)
    {
        if(!preg_match('/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/',$str))
        {
            $this->form_validation->set_message('valid_date','date format is not valid. dd-mm-yyyy');
            return false;
        }

        else
        {
            return true;
        }
    }

}

?>
<?php
//script of model
class Siswa_model extends CI_Model
{
    private $primary_key='id';
    private $table_name='siswa';

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

    function get_paged_list($limit=10,$offset=0,$order_column='',$order_type='asc')
    {
        if(empty($order_column) || empty ($order_type))
        {
            $this->db->order_by($this->primary_key,'asc');
        }

        else
        {
            $this->db->order_by($order_column,$order_type);
        }

        return $this->db->get($this->table_name,$limit,$offset);
    }

    function count_all()
    {
        return $this->db->count_all($this->table_name);
    }

    function get_by_id($id)
    {
        $this->db->where($this->primary_key,$id);
        return $this->db->get($this->table_name);
    }

    function save($person)
    {
        $this->db->insert($this->table_name,$person);
        return $this->db->insert_id();
    }

    function update($id,$person)
    {
        $this->db->where($this->primary_key,$id);
        $this->db->update($this->table_name,$person);
    }

    function delete($id)
    {
        $this->db->where($this->primary_key,$id);
        $this->db->delete($this->table_name);
    }
}
?>  

1 个答案:

答案 0 :(得分:1)

function add() {
...........
$siswa = array('nama'=>$this->input->post('nama'),
            'alamat'=>$this->input->post('alamat'),
            'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
            'tanggal_lahir'=>date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));

        $this->validation->id = $id;
        $data['siswa']=$this->siswa_model->add($id,$siswa);
        redirect('siswa/index/add_success');

$ id未在任何地方声明。

你打电话给$ this-&gt; siswa_model-&gt; add($ id,$ siswa)但siswa_model中不存在函数add()(它叫做save​​())