【问题标题】:whats wrong with my code at the moment I cant update one row?目前我的代码有什么问题我无法更新一行?
【发布时间】:2017-07-25 17:43:05
【问题描述】:

我如何比较我的 id 和我的 hash id 是否相等然后更新?问题在于,当我想更新任何行时,因为没有检测到我的隐藏输入并更新任何行上的相同内容,所以不合适吗?我该如何解决?这就是我到目前为止所得到的。我正在使用数据表 jquery 和 codeigniter http://alasksoft.tk/login 凭据管理员 - 管理员 当我想在我的 php 控制器中访问此输入 return '<input type="hidden" name="id" value="'+data+'" >'; 时,我看不到,因为我没有遇到厄运,因为数据表 jquery 隐藏了它,如何在不显示的情况下修复它?

更新控制器

public function updateProduct(){
        $descripcion = $this->input->post('description');
        $cost_price =  $this->input->post('cost_price');
        $selling_price = $this->input->post('selling_price');
        $wprice = $this->input->post('wprice');
        $min_stock = $this->input->post('min_stock');
        $stock = $this->input->post('stock');
        $max_stock = $this->input->post('max_stock');
        $data = array(
            'descripcion' => $descripcion,
            'precio_compra' => $cost_price,
            'precio_venta' => $selling_price,
            'precio_mayoreo' => $wprice,
            'existencia_minima' => $min_stock,
            'existencia' => $stock,
            'existencia_maxima' => $max_stock
        );
        if ($data['existencia'] > $data['existencia_minima']) {
            $this->json(array('min_stock' => 'el stock no puede ser mayor al min'));
        }elseif ($data['existencia_maxima'] < $data['existencia']) {
            $this->json(array('max_stock' => 'el stock no puede ser mayor al max'));
        }else{
           $this->products->updateProduct($data);
            $this->json(array('msg' => 'successfully added'));
           $this->json($data);
        }
    }

更新模型

public function isExistsProduct($data){
        $this->db->select('descripcion');
        $this->db->from('storelte_articulos');
        $this->db->where('descripcion',$data['descripcion']);
        $query = $this->db->get();
        return $query->num_rows() == 0 ? false : true;
    }

public function updateProduct($data) { 
        $this->db->update('storelte_articulos',$data); 
        $this->db->where('md5(id)',hash('md5', $this->input->post('id')));
    }

ajax 表

 var table = $('#example').DataTable({
        "lengthChange": false,
        responsive: true,
        dom: 'Blfrtip',
        buttons: [{
             extend: 'excelHtml5',
             exportOptions:{
                columns: [1,2,3,4,5,6]
             }
        },{
            extend: 'csvHtml5',
            exportOptions:{
                columns: [1,2,3,4,5,6]
            }
        },{
            extend: 'pdf',
            exportOptions: {
                columns: [1,2,3,4,5,6]
            }
        }],
        ajax: {
            url: URL_GET_DATATABLE,
            type: 'POST',
        },
        columnDefs:[{
            targets: -1,
            data: null,
            defaultContent: "<a href='#'><span class='glyphicon glyphicon-pencil'></span></a>"

        },{
            targets: 7,
            render: function (data) {
                return (data == 1) ? "<span class='label label-success'>active</span>":"<span class='label label-danger'>inactive</span>";
            }
        },
        {
            targets: 0,
            visible: false,
            render: function (data) {
               return  '<input type="hidden" name="id" value="'+data+'" >';
            }
        }],
        fnRowCallback: function (data,nRow) {
            if (nRow[7] == 0) {
                $(data).css({'background-color':'#f2dede'});
            }else if(nRow[7] == 1){
                $(data).css({'background-color':'#dff0d8'});
            }else{

            }
        }
    });

【问题讨论】:

  • 请按照minimal reproducible example 将其缩小到特定问题的最小表示。然后更新问题陈述,使其与实际代码相关。很难理解你的问题是什么
  • 问题是当我尝试更新任何项目作为我的控制器 md5(id) = hash 然后 update ,但是当我这样做更新整个数据库时,我该如何修复它?代码更新
  • where 语句应在updateProduct 中的update 之前
  • 还是没用

标签: jquery oop datatables codeigniter-3 php-7


【解决方案1】:

实际上你的问题有点难以理解,但我的经验是这行代码

public function updateProduct($data) { 
    $this->db->update('storelte_articulos',$data); 
    $this->db->where('md5(id)',hash('md5', $this->input->post('id')));
}

您首先更新表而不是使用 where 所以将其更改为

public function updateProduct($data) { 
  $this->db->where('md5(id)',hash('md5', $this->input->post('id')));  
  $this->db->update('storelte_articulos',$data); 
}

【讨论】:

    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2014-04-18
    相关资源
    最近更新 更多