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();
}