【问题标题】:CodeIgniter Update query not workingCodeIgniter 更新查询不起作用
【发布时间】:2016-06-01 10:20:47
【问题描述】:

我想从系统中解雇一名员工。当点击终止按钮时,它会弹出一个模式询问是否要终止或取消。如果终止数据库值 resign 应该更新为 0,但现在按钮不起作用。

这是我的代码

控制器

public function ajax_list()
{
    $list = $this->employees->get_datatables();

    $data = array();
    $no = $_POST['start'];
    foreach ($list as $emp) {
        $no++;
        $row = array();
        $row[] = $emp->employee_id;
        $row[] = $emp->name;
        $jid = $emp->job_title;
        $desigdata = $this->employees->GetJobTitlebyID($jid);
        $row[] = $desigdata->desc;
        $did = $emp->department;
        $deptdata = $this->employees->GetDepartmentbyID($did);
        $row[] = $deptdata->title;
        $secid = $emp->section;
        $secdata = $this->employees->GetSectionbyID($secid);
        $row[] = $secdata->desc;


        //add html for action
        $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void()"  onclick="terminate_emp('."'".$emp->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Terminate</a>';


        $data[] = $row;
    }

    $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->employees->count_all(),
        "recordsFiltered" => $this->employees->count_filtered(),
        "data" => $data,
    );

    echo json_encode($output);
}


public function ajax_terminate()
    {
        $this->_validate();
        $data = array(
            'resign' => $this->input->post('resign'),

        );
        $this->employees->update(array('id' => $this->input->post('id')), $data);
        echo json_encode(array("status" => TRUE, "id" => $this->input->post('id')));
    }

型号

 function terminate_emp($data)
 {
     $this->db->where('resign', 0);
     $this->db->update('employees', $data);
 }

查看

function terminate_emp(id)
    {
        save_method = 'update';
        $('#form')[0].reset(); 
        $('.form-group').removeClass('has-error'); 
        $('.help-block').empty(); 

        //Ajax Load data from ajax
        $.ajax({
            url : "<?php echo site_url('employees_con/ajax_terminate/')?>/" + id,
            type: "GET",
            dataType: "JSON",
            success: function(data)
            {
                $('[name="id"]').val(data.id);

                if(data.resign == 1)
                {
                    //$('[name="resign"]').val(data.resign);
                    $('#resign').prop('checked', true);
                }
                $('[name="resign"]').val(data.resign);

                $('#modal_formterminate').modal('show'); // show bootstrap modal when complete loaded
                $('.modal-title').text('Terminate Employee'); // Set title to Bootstrap modal title

            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error get data from ajax');
            }
        });
    }



<div class="modal fade" id="modal_formterminate" role="dialog">

    <div class="modal-dialog modal-full" style="max-width: 600px">

    <div class="modal-content">

        <div class="modal-header bg-blue-steel bg-font-blue-steel">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h3 class="modal-title bold uppercase">Person</h3>
        </div>

        <div class="modal-body form">
            <form action="#" id="form" class="form-horizontal">
                <input type="hidden" value="" name="id"/>
                <div class="form-body">

                    <div id="empWizard">


                        <p style="color: #0000cc"><b>Are You sure to Terminate this employee</b></p>

                    </div>
                </div>

            </form>
        </div>

        <div class="modal-footer">
            <button type="button" id="btnSaveterminate" onclick="save()" class="btn btn-primary">Terminate</button>
            <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
        </div>

    </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->

</div>

【问题讨论】:

    标签: php mysql ajax codeigniter


    【解决方案1】:

    在控制器中

    $this->employees->update(array('id' => $this->input->post('id')), $data);
    

    您正在传递两个参数来更新模型的函数,一个是数组,另一个是 $data,它也是一个数组。

    但在模型中,

    function terminate_emp($data)
    {
        $this->db->where('resign', 0);
        $this->db->update('employees', $data);
    }
    

    你只是在更新函数中接受一个参数。

    【讨论】:

      【解决方案2】:

      首先,您通过 AJAX 和控制器函数 ajax_terminate() 发出 GET 请求,您正在使用 POST 访问变量。你resign 值没有通过ajax 并且你正试图获得控制器功能ajax_terminate()。见以下代码:-

      function terminate_emp(id)
      {
          save_method = 'update';
          $('#form')[0].reset(); 
          $('.form-group').removeClass('has-error'); 
          $('.help-block').empty(); 
      
          //Ajax Load data from ajax
          $.ajax({
              url : "<?php echo site_url('employees_con/ajax_terminate/')?>/",
              type: "POST",
              dataType: "JSON",
              data: {id:id,resign:YOUR_RESIGN_VALUE}
              success: function(data)
              {
                  $('[name="id"]').val(data.id);
      
                  if(data.resign == 1)
                  {
                      //$('[name="resign"]').val(data.resign);
                      $('#resign').prop('checked', true);
                  }
                  $('[name="resign"]').val(data.resign);
      
                  $('#modal_formterminate').modal('show'); // show bootstrap modal when complete loaded
                  $('.modal-title').text('Terminate Employee'); // Set title to Bootstrap modal title
      
              },
              error: function (jqXHR, textStatus, errorThrown)
              {
                  alert('Error get data from ajax');
              }
          });
      }
      

      将模型更改为

      function terminate_emp($where,$data)
      {
          $this->db->where($where);
          $this->db->update('employees', $data);
      }
      

      【讨论】:

      • $this-&gt;_validate(); 在做什么?
      • 其实不需要
      • 在模态的最后一行添加echo $this-&gt;db-&gt;last_query() 然后告诉我它是什么
      • 没有任何回应
      猜你喜欢
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2015-01-19
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多