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

时间:2017-01-24 17:06:31

标签: php codeigniter insert invoice

我疯了,这是我的代码

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
        }
    }
}

要添加发票,我没有任何问题,我无法使其工作的是更新该发票,如果添加了产品或更改了详细信息,是否有任何想法?非常感谢你&lt; 3

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

1 个答案:

答案 0 :(得分:1)

您的update_batch()函数不知道要更新的内容,因为您忘了

定义一个键。它可能看起来像这样(取决于您需要使用哪个ID):

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

有关更新数据的更多信息:here

修改

您还需要从$this->db->update_batch()循环中取出for函数

在你的for循环中你只需要添加到数组$update,当你每次循环递增时你都会被覆盖。有关向数组添加数据的更多信息here