Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果未找到数据则插入,如果找到数据则更新-Codeigniter PHP_Php_Codeigniter_Insert_Invoice - Fatal编程技术网

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

如果未找到数据则插入,如果找到数据则更新-Codeigniter PHP,php,codeigniter,insert,invoice,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' =&g

我快疯了,这是我的密码

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
        }
    }
}
要添加发票我没有问题,我不能让它工作的是更新发票,如果产品添加或细节更改,有什么想法?非常感谢您的update\u batch函数不知道要更新什么,因为您忘记了更新

定义一个键。它可能是这样的,具体取决于您需要使用哪个ID:

$this->db->update_batch($update, 'detalleremitos', 'id_factura');
有关更新数据的更多信息:

编辑:

您还需要将$this->db->update\u批处理函数从for循环中去掉


在for循环中,您只需要将$update添加到数组中,此时每次循环递增时都会覆盖该数组。更多关于向数组添加数据的信息

感谢您的帮助,不幸的是,我收到了错误消息:调用成员函数result\u array on array我找不到错误将$this->db->update\u batch函数从for循环中取出请查看这些链接谢谢您的帮助,我遵循他的步骤,但我无法摆脱错误:消息:调用数组上的成员函数num_rows我觉得很愚蠢,为这么多的麻烦感到抱歉,但我非常感谢能够找到解决方案您很接近,在for循环中,您只需添加到数组$update,此时,每次循环增加时,您都会覆盖这些内容,这里的内容更多http://php.net/manual/en/function.array-push.php