Insert with several records with PDO PHP using a loop

0

I have this "add" function, which adds a product code 'X' in a table called "INVENTORIES", works well for me, but I want to add a fragment of code in PHP that I insert in a table called "PRECIOSXPROD" the product code for 'Y' times according to the price list are found with the function 'showLists', whose result I store in a $ t_list arrangement and I go through it with a foreach, when doing an echo of an index it works well for me, that it indicates that the array gets the data, but by putting the lines of code that theoretically must do that insert, I do not get anything in the target table.

Am I misusing the sentences or the syntax must be corrected at some point?

** UPDATE 4/17/18 The function inserts a single record with the highest number registered in the number of price lists, that is, if there are three price lists registered, only inserts the product code in the last registered list (as if it did not insert for each iteration of the foreach). Many thanks to @A. Cedano for the help given.

static public function add($tabla, $datos) {


        $stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (ID_PRODUCTO, DESC_PROD, EXIST_MIN, DIAS_ENTREGA, ESQ_IMP, ID_LIN, SERVICIO, IMG_PRODUCTO, COSTO_PROM, COSTO_ULTIMO, EXIST) "
                . "VALUES (:ID_PRODUCTO, :DESC_PROD, :EXIST_MIN, :DIAS_ENTREGA, :ESQ_IMP, :ID_LIN, :SERVICIO, :IMG_PRODUCTO, 0.0, 0.0, 0.0 )");

        $stmt->bindParam(":ID_PRODUCTO", $datos["ID_PRODUCTO"], PDO::PARAM_STR);
        $stmt->bindParam(":DESC_PROD", $datos["DESC_PRODUCTO"], PDO::PARAM_STR);
        $stmt->bindParam(":EXIST_MIN", $datos["EXIST_MIN"], PDO::PARAM_STR);
        $stmt->bindParam(":DIAS_ENTREGA", $datos["DIAS_ENTREGA"], PDO::PARAM_INT);
        $stmt->bindParam(":ESQ_IMP", $datos["ESQ_IMP"], PDO::PARAM_INT);
        $stmt->bindParam(":ID_LIN", $datos["ID_LIN"], PDO::PARAM_INT);
        $stmt->bindParam(":SERVICIO", $datos["SERVICIO"], PDO::PARAM_INT);
        $stmt->bindParam(":IMG_PRODUCTO", $datos["IMG_PRODUCTO"], PDO::PARAM_STR);

       /*--
        * OBTENEMOS EL TOTAL DE LISTAS DE PRECIOS PARA PODER AGREGAR EL CÒDIGO POR CADA UNA
        * --*/ 

        $producto=$datos["ID_PRODUCTO"];
        $item = NULL;
        $valor = NULL;
        $t_listas= ControladorListaPrecios::mostrarListas($item, $valor);


        foreach ($t_listas as $key => $value) {
            //echo $producto.' '.$value["ID_LISTA"].'<br>'; /*HASTA ACÀ FUNCIONA, LO QUE NO FUNCIONA SON LAS SIGUIENTES $stmt2*/
            $stmt2= Conexion::conectar()->prepare("INSERT INTO PRECIOSXPROD (ID_PRODUCTO, ID_LISTA, PRECIO) "
                . "VALUES (:ID_PRODUCTO, :ID_LISTA, 0.0)");
            $stmt2->bindParam(":ID_PRODUCTO", $producto, PDO::PARAM_STR);
            $stmt2->bindParam(":ID_LISTA", $value["ID_LISTA"], PDO::PARAM_INT);         
            $stmt2->execute();

        }

        /*HE AGREGADO EL !$stmt2 SUGERIDO EN ANTERIOR RESPUESTA PERO EN ESTA PARTE DEL CÓDIGO*/
        if ($stmt->execute() && !$stmt2->execute()) {
            return "ok";
        } else {
            return "error";
        }

        $stmt->close();
        $stmt = NULL;
    }
    
asked by Germanddy Hernández 16.04.2018 в 21:30
source

1 answer

0

** 4/18/18

FUNCTIONAL CODE:

When making some changes, the code does what I'm looking for, now, I do not know if it's the best practice, but I'll put it below.

static public function add($tabla, $datos) {


        $stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (ID_PRODUCTO, DESC_PROD, EXIST_MIN, DIAS_ENTREGA, ESQ_IMP, ID_LIN, SERVICIO, IMG_PRODUCTO, COSTO_PROM, COSTO_ULTIMO, EXIST) "
                . "VALUES (:ID_PRODUCTO, :DESC_PROD, :EXIST_MIN, :DIAS_ENTREGA, :ESQ_IMP, :ID_LIN, :SERVICIO, :IMG_PRODUCTO, 0.0, 0.0, 0.0 )");

        $stmt->bindParam(":ID_PRODUCTO", $datos["ID_PRODUCTO"], PDO::PARAM_STR);
        $stmt->bindParam(":DESC_PROD", $datos["DESC_PRODUCTO"], PDO::PARAM_STR);
        $stmt->bindParam(":EXIST_MIN", $datos["EXIST_MIN"], PDO::PARAM_STR);
        $stmt->bindParam(":DIAS_ENTREGA", $datos["DIAS_ENTREGA"], PDO::PARAM_INT);
        $stmt->bindParam(":ESQ_IMP", $datos["ESQ_IMP"], PDO::PARAM_INT);
        $stmt->bindParam(":ID_LIN", $datos["ID_LIN"], PDO::PARAM_INT);
        $stmt->bindParam(":SERVICIO", $datos["SERVICIO"], PDO::PARAM_INT);
        $stmt->bindParam(":IMG_PRODUCTO", $datos["IMG_PRODUCTO"], PDO::PARAM_STR);




        if ($stmt->execute()) {

           /*--
            * OBTENEMOS EL TOTAL DE LISTAS DE PRECIOS PARA PODER AGREGAR EL CÒDIGO DE PRODUCTO NUEVO POR CADA UNA.
            * 
            * 18/04/18 AL PASAR ESTE CÓDIGO DENTRO DE ESTA CONDICIÓN FUNCIONÓ PARA AGREGAR
            * CADA CÓDIGO CREADO EN CADA LISTA DE PRECIOS--*/        


            $producto=$datos["ID_PRODUCTO"];
            $item = NULL;
            $valor = NULL;
            $t_listas= ControladorListaPrecios::mostrarListas($item, $valor);
            $contaListas= count($t_listas);

            $stmt2= Conexion::conectar()->prepare("INSERT INTO PRECIOSXPROD (ID_PRODUCTO, ID_LISTA, PRECIO) "
                    . "VALUES (:ID_PRODUCTO, :ID_LISTA, 0.0)");
            $stmt2->bindParam(":ID_PRODUCTO", $producto, PDO::PARAM_STR);

            /*AL USAR TAMBIÉN EL FOR CON LA CANTIDAD DE REGISTROS PARA REALIZAR LAS INSERCIONES, AL SER ENTERO EL CAMPO ID_LISTA,
              NO GENERÓ NINGÚN RESULTADO NO BUSCADO*/

            for($i=1;$i<=$contaListas;$i++) {                
                $stmt2->bindParam(":ID_LISTA", $i, PDO::PARAM_INT);         
                $stmt2->execute();
            }
            /*-------*/



            return "ok";
        } else {
            return "error";
        }

        $stmt->close();
        $stmt = NULL;
    }
    
answered by 18.04.2018 / 18:53
source