无法将变量会话插入数据库

时间:2015-02-16 14:28:36

标签: mysql codeigniter session

这是控制器

public function add(){

    $npp=$this->session->userdata('NPP');

    $mpwp=$this->input->post('mpwp');

    $ket=$this->input->post('ket');

    $data = array(
       'npp'=>$npp,
       'tgl_pengajuan' => date('y-m-d'),
       'ket' => $ket,
        'status' => 'P' 
       'mpwp'=>$mpwp,
     );

    $this->Cutimpwp_Model->add($data);

    redirect('cutiMpwp');
  }

这是Model

function add($data) {
  $this->db->insert('cuti_mpwp',$data);
}

这是视图

<div class="form_row">
<label class="field_name">mpwp</label>
<div class="field">
<input type="text"  name="mpwp"  class="input-large"   value="<?php echo set_value('mpwp'); ?>" placeholder="masukan nama"> 
</div>
</div>
<div class="form_row">
<label class="field_name">Keterangan (MAKS 50 KARAKTER)</label>
<div class="field">
<textarea  placeholder="KETERANGAN CUTI" id="ket" name="ket" class="span12" cols="40" rows="5"></textarea>
</div>

</div>

这是我在数据库中的专栏

  

id | npp | tgl_pengajuan | ket |状态| mpwp

没有错误,但无法插入

抱歉,我忘了把标签表放在这里 所以这是我的全部观点

<link rel="stylesheet" href="<?php echo base_url();?>css/themes/jquery.ui.all.css" type="text/css" />
<script>  $(document).ready(function() {
  $( ".datepicker" ).datepicker(); });
  function save(){  $.ajax({
  url:'<?php echo base_url(); ?>cutimpwp/add/',     
  type:'POST',
  data:$('#frmsave').serialize(),
  success:function(data){ 
      if(data!=''){
       $( "#infodlg" ).html(data);
       $( "#infodlg" ).dialog({ title:"Info...", draggable: false});           
    } else {
       window.location="<?php echo base_url() ?>cutimpwp";   }
   } });  }
   function confirmdlg(){
      $("#confirm").dialog({
       resizable: false,
       modal: true,
       title:"Info...",
       draggable: false,
       width: 'auto',

       height: 'auto',
       buttons: {
       "Ya": function(){
         save();   
          $(this).dialog("close");
          window.location="<?php echo base_url() ?>cutimpwp";
        },
        "Tutup": function(){
           $(this).dialog("close");
        }
       }
      }); } </script>
      <div class="span6">
                    <div class="well grey">
                        <div class="well-header">
                            <h5>Tambah Cuti  </h5>
                        </div>
            <div class="well-content no-search">
                            <form id="frmsave" name="frmsave"  class="form-validate">


                    <h3>Detail </h3>


               <div class="form_row">
                                    <label class="field_name">mpwp</label>
                                    <div class="field">
                                       <input type="text"  name="mpwp"  class="input-large"   value="<?php echo set_value('mpwp'); ?>" placeholder="masukan nama"> 
                </div>
               </div>

               <div class="form_row">
                                    <label class="field_name">Keterangan (MAKS 50 KARAKTER)</label>
                                    <div class="field">
                                        <textarea  placeholder="KETERANGAN CUTI" id="ket" name="ket" class="span12" cols="40" rows="5" value="<?php echo set_value('ket'); ?>"></textarea>
                                    </div>
                                </div>


               <div class="form_row">
                                    <div class="field">
                                        <a onclick="return confirmdlg()" class="blue btn">Submit</a>
                                        <a  href="<?php echo base_url() ?>cuti_mpwp" class="red btn">Cancel</a>
                                    </div>
                                </div>

                                 </form>
            </div>
            </div>
            </div>
            <div id="confirm" style="display:none"> Anda Ingin Meyimpan data ini</div>     

并关注您的教师

我的控制器

    public function add()
        { if($this->session->userdata('LOGIN')=='TRUE')
     { $this->load->library('form_validation');
      $this->form_validation->set_rules('mpwp', 'mpwp');
       if ($this->form_validation->run() == false) {
        $data['view'] = 'cutimpwp/add';
         $data['judul']='';
        $this->load->view('index',$data);
        }else {$this->load->model('Cutimpwp_Model');
   $this->Cutimpwp_Model->add();
  redirect('cutimpwp'); }}}

控制器

 `public function add() {
  $npp = $this->session->userdata('NPP');
  $mpwp = $this->input->post('mpwp');
  $ket = $this->input->post('ket');
  $data = array( 'npp'=> $npp,
  'tgl_pengajuan' => date('y-m-d'),
  'ket' => $ket,
  'status' => 'P', 
  'mpwp'=> $mpwp);
   $this->db->insert('Cutimpwp_Model', $data);}`

1 个答案:

答案 0 :(得分:0)

为什么不在这样加载模型的地方做这件事。另外在控制器中看不到任何表单验证。如果提交的话,你需要这样做。

请记得在视图上使用表单标签没有看到任何内容。

模型

public function add() {

  $npp = $this->session->userdata('NPP');

  $mpwp = $this->input->post('mpwp');

  $ket = $this->input->post('ket');

  $data = array(
      'npp' => $npp,
      'tgl_pengajuan' => date('y-m-d'),
      'ket' => $ket,
      'status' => 'P'
      'mpwp'=> $mpwp
    );

  $this->db->insert('table', $data);    
}

提交表单时,您需要运行codeigniter表单验证。在表单验证负载模型函数的成功部分。

您还可能需要在routes.php中设置函数名称

控制器:

<?php

class Name extends CI_Controller {

  public function index() {

  }

  public function add() {
    $this->load->library('form_validation');

    $this->form_validation->set_rules('mpwp', 'mpwp');

    if ($this->form_validation->run() == false) {

      // do something

    } else {

      $this->load->model('modelname');

      $this->modelname->add();

      redirect('yourpage');

    }
  }
}