Error retrieving the last ID of a record and using it - codeigniter - PHP

0

Hol I have problems with my code PHP , the header if REGISTER that means that everything is fine but when I want to recover the ID I get the error I do not know if the error is in my controller when calling that id or in my model when it comes to consulting about the last ID .

Driver:

   public function guardar_venta() {

        $id_cliente = $this->input->post('id_cliente');
        $descuento = $this->input->post('descuento');
        $efectivo = $this->input->post('efectivo');
        $vuelto = $this->input->post('vuelto');
        $total = $this->input->post('total');
        $detalle = $this->input->post('detalle');

        $this->venta->guardar_cabecera($id_cliente, $descuento, $efectivo, $vuelto, $total);

        $id_cabecera = $this->input->post('id_venta');

        if (isset($id_cabecera)) {

            if ($detalle != "[") {
                $array_detalle = json_decode($detalle);
                foreach ($array_detalle as $objeto) {
                    $id_producto = $objeto->id_producto;
                    $cantidad = $objeto->cantidad;
                    $precio = $objeto->precio;
                    $subtotal = $objeto->subtotal;

                    $this->venta->guardar_detalle($id_cabecera, $id_producto, $cantidad, $precio, $subtotal);
                }
            }
            echo 1;
        }
    }

Model:

   public function guardar_cabecera($id_cliente, $descuento, $efectivo, $vuelto, $total) {
        $this->db_caja->reconnect();
        $this->db_caja->query("INSERT INTO venta VALUES(null,'$id_cliente','$descuento','$efectivo','$vuelto','$total',NOW());");
        return $this->db_caja->insert_id();
    }

    public function guardar_detalle($id_cabecera, $id_producto, $cantidad, $precio, $subtotal) {
        $this->db_caja->reconnect();
        $query = $this->db_caja->query("INSERT INTO detalle_venta VALUES (null,'$id_cabecera','$id_producto','$cantidad','$precio','$subtotal');");
        return $query->result();
    }
    
asked by Ivan More Flores 14.06.2017 в 19:00
source

1 answer

0

SOLUTION AT THE TIME OF CALLING IT WAS INCORRECT:

I had to put an identifier to the variable that I was going to recover, and this same one already brought me the ID , in the following line it was just to match it and that already solves the problem.

CONTROALDOR:

public function guardar_venta() {

        $id_cliente = $this->input->post('id_cliente');
        $descuento = $this->input->post('descuento');
        $efectivo = $this->input->post('efectivo');
        $vuelto = $this->input->post('vuelto');
        $total = $this->input->post('total');
        $detalle = $this->input->post('detalle');

        $id_cabe = $this->venta->guardar_cabecera($id_cliente, $descuento, $efectivo, $vuelto, $total);

        $id_cabecera = $id_cabe;

        if (isset($id_cabecera)) {

            if ($detalle != "[") {
                $array_detalle = json_decode($detalle);
                foreach ($array_detalle as $objeto) {
                    $id_producto = $objeto->id_producto;
                    $cantidad = $objeto->cantidad;
                    $precio = $objeto->precio;
                    $subtotal = $objeto->subtotal;

                    $this->venta->guardar_detalle($id_cabecera, $id_producto, $cantidad, $precio, $subtotal);
                }
            }
            echo 1;
        }
    }
    
answered by 14.06.2017 / 19:07
source