Insert data with iteration in php

2

I have the following function in the model that I want to enter in the database:

public function agregarBolsasModel($datos, $tabla){
         $stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:uno1, :dos2, :tres3)");
         $stmt -> bindParam(":uno1", $datos["loteNum"], PDO::PARAM_INT);
         $stmt -> bindParam(":dos2", $datos["cajaNum"], PDO::PARAM_INT);
         $stmt -> bindParam(":tres3", $datos["valoresBox"], PDO::PARAM_INT);
         if($stmt->execute()){

            return "ok";
         }else{
             return "error";
         }
         $stmt->close();
    }

The question that the third value or ": tres3" is an array of data that happened with the following array: (Passes to a controller, but here I omit it)

 public function agregarEnCaja(){
       $datos = array("loteNum" => $this -> loteIngreso, //101
                      "cajaNum"=> $this -> cajaIngreso,  //1
                      "valoresBox"=> $this -> valoresCaja); //[1,2,3]
        $respuesta=$respuesta=GestorOIT::agregarBolsasController($datos);
        echo $respuesta;
   }

How can I iterate the element 3 in order to enter the data depending on its size, for example, the data array is like this:

$datos={loteNum:101, cajaNum:1, valoresBox[1,2,3]}

I want it to be in the function like this, depending on its size:

INSERT INTO tabla(lote, caja, bolsa)VALUES(101,1,1);
INSERT INTO tabla(lote, caja, bolsa)VALUES(101,1,2);
INSERT INTO tabla(lote, caja, bolsa)VALUES(101,1,3);

Any help would be appreciated a lot.

EDITED

Try this way but nothing

public function agregarBolsasModel($datos, $tabla){
        $datosL=array($datos["valoresBox"]);
        for($p=0; $p<sizeof($datosL); $p++){
            $stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:uno1, :dos2, :tres3)");
            $stmt -> bindParam(":uno1", $datos["loteNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":dos2", $datos["cajaNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":tres3", $datosL[$p], PDO::PARAM_INT);
            $stmt->execute();
        }

          $stmt->close();
 }
    
asked by Baker1562 21.02.2018 в 04:17
source

2 answers

0

In the end I could solve it in the following way:

public function agregarBolsasModel($datos, $tabla){
        $stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:uno1, :dos2, :tres3)");
        for($p=0; $p<count($datos["valoresBox"]); $p++){
            $stmt -> bindParam(":uno1", $datos["loteNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":dos2", $datos["cajaNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":tres3", $datos["valoresBox"][$p], PDO::PARAM_INT);
            $stmt->execute();

        }
        $stmt = null;
    }
    
answered by 21.02.2018 / 20:01
source
2

Use the count ($ array) PHP function to get the size of an array: Change

for($p=0; $p<sizeof($datosL); $p++)

for

for($p=0; $p < count($datosL); $p++)

staying like this:

public function agregarBolsasModel($datos, $tabla){
        $datosL=array($datos["valoresBox"]);
        for($p=0; $p < count($datosL); $p++){
            $stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:uno1, :dos2, :tres3)");
            $stmt -> bindParam(":uno1", $datos["loteNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":dos2", $datos["cajaNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":tres3", $datosL[$p], PDO::PARAM_INT);
            $stmt->execute();
        }

          $stmt->close();
 }
    
answered by 21.02.2018 в 19:31