dongou4052 2017-01-24 17:06
浏览 79

如果没有找到数据则插入,如果找到数据则更新 - Codeigniter PHP

I'm going crazy, this is my code

public function add_remito()
{
    $post = $this->input->post();
    $data = array(
            'id_cliente' => $post['id_cliente'],
            'razonsocial_cliente' => $post['nombre_cliente'],
            'fecha_remito'  => $post['fecha'],
            'total_remito'  => $post['total_remito']
        );
    $this->db->insert('remitosclientes', $data);
    $id = $this->db->insert_id();
    if ($id > 0) {
        for ($i=0;$i<count($post['nombre_producto']);$i++) {
            $data1 = array(
              'id_factura' => $id,
              'id_product' => $post['id_producto'][$i],
              'nombre_producto' => $post['nombre_producto'][$i],
              'cantidad' => $post['stock'][$i],
              'precio' => $post['precio'][$i],
              'color' => $post['color'][$i],
              'talle' => $post['talle'][$i]
              );
            $this->db->insert('detalleremitos', $data1);

            if ($this->db->insert_id()) {
                $this->db->select('*');
                $this->db->where('id_producto', $post['id_producto'][$i]);
                $query = $this->db->get('productos');
                $row = $query->row();
                $quantity_in_stock = $row->stock;

                $new_quantity_in_stock = intval($quantity_in_stock) - intval($post['stock']);

                $this->db->where('id_producto', $post['id_producto'][$i]);
                $this->db->update('productos', array('stock' => $new_quantity_in_stock ));
            }

            $data2 = array(
                        'id_reg_producto' => $post['id_producto'][$i],
                        'reg_nombre_producto' => $post['nombre_producto'][$i],
                        'reg_cantidad_ant' => $quantity_in_stock,
                        'reg_cantidad' => $new_quantity_in_stock,
                        'reg_fecha' => $post['fecha'],
                        'reg_motivo' => "Venta"
                    );

            $this->db->insert('regstocks', $data2);
        }

        $this->session->set_flashdata('success', 'Data Save Successfully.');
        return redirect('venta/remitos');
    }
}

public function update_remito()
{
    $post = $this->input->post();

    $idremito = $this->uri->segment('3');

    $data = array(
          'id_cliente' => $post['id_cliente'],
          'razonsocial_cliente' => $post['nombre_cliente'],
          'fecha_remito'  => $post['fecha'],
          'total_remito'  => $post['total_remito']
      );

    $this->db->where('id_remito', $idremito);
    $this->db->update('remitosclientes', $data);

    $q = $this->db->select('id_factura')->get('detalleremitos')->result_array();

    $db_id=array();
    $update=array();

    foreach ($q as $key => $value) {
        $db_id[$key]=$value['id_factura'];
    }

    for ($i=0;$i<count($post['id_producto']);$i++) {
        $update = array(
      'id_factura' => $idremito,
      'id_producto' => $post['id_producto'][$i],
      'nombre_producto' => $post['nombre_producto'][$i],
      'cantidad' => $post['stock'][$i],
      'precio' => $post['precio'][$i],
      'color' => $post['color'][$i],
      'talle' => $post['talle'][$i]
      );

        $update_query = $this->db->where_in('id_factura', $idremito)
         ->get('detalleremitos')->result  ();
        var_dump($update_query);
        if (count($update_query) > 0) {
          $this->db->insert('detalleremitos', $update);//update if ids exist

        } else {
            $this->db->update('detalleremitos', $update, $idremito);//insert if does not exist
        }
    }
}

To add the invoice I have no problems, what I can not make it work is to update that invoice, if products were added or details changed, any ideas? Thank you very much <3

https://github.com/outthesystem/facturacion-codeigniter

  • 写回答

1条回答 默认 最新

  • doujianwan7570 2017-01-24 17:56
    关注

    your update_batch() function doesn't know what to update, because you forgot to

    define a key. It could look like this (depending which ID you need to use):

    $this->db->update_batch($update, 'detalleremitos', 'id_factura');
    

    more about updating data: here

    edit:

    you further need to take the $this->db->update_batch() function out of the for loop

    and

    in your for loop you just need to add to the array $update, which at the moment you are overwriting each time the loop increments. More about adding data to arrays here

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)