I try to insert multiple rows with different values but I don’t know why doesn’t work
$id_poliza = $this->dbConnect->lastInsertId(); //With this, I save last insert id from last executed sql statement.
if($this->dataNewPoliza['d724'] == 'on')
$disp1 = 1;
else
$disp1 = 0;
if($this->dataNewPoliza['d58'] == 'on')
$disp2 = 1;
else
$disp2 = 0;
if(($this->dataNewPoliza['especial']) == '')
$disp3 = 0;
else
$disp3 = $this->dataNewPoliza['especial'];
if(($this->dataNewPoliza['ticket']) == '')
$disp4 = 0;
else
$disp4 = $this->dataNewPoliza['ticket'];
$sqlDispPol = "INSERT INTO disponibilidad_poliza (Id_Poliza, Id_Disponibilidad, Valor)
VALUES (:idP,:idD1,:val1),
(:idP,:idD2,:val2),
(:idP,:idD3,:val3),
(:idP,:idD4,:val4)";
$resultDisp = $this->dbConnect->prepare($sqlDispPol) or die ($sqlDispPol);
$resultDisp->bindParam(':idP',$id_poliza,PDO::PARAM_INT);
$resultDisp->bindParam(':idD1',1,PDO::PARAM_INT);
$resultDisp->bindParam(':val1',$disp1,PDO::PARAM_STR);
$resultDisp->bindParam(':idD2',2,PDO::PARAM_INT);
$resultDisp->bindParam(':val2',$disp2,PDO::PARAM_STR);
$resultDisp->bindParam(':idD3',3,PDO::PARAM_INT);
$resultDisp->bindParam(':val3',$disp3,PDO::PARAM_STR);
$resultDisp->bindParam(':idD4',4,PDO::PARAM_INT);
$resultDisp->bindParam(':val4',$disp4,PDO::PARAM_STR);
if($resultDisp->execute()) {
//more code
}
I want save from this form:
Table disponibilidad_poliza
Id_Poliza Id_Disponibilidad Valor
________________________________________
1 1 1
1 2 0
1 3 especial10
1 4 7
Note: $disp1 and $disp2 are checkboxes, $disp3 and $disp4 are inputs text
Maybe this way is incorrect and I appreceate If you put another way to do this please.
Thanks in advance.
Use bindValue() instead of bindParam(). It takes the same arguments but will work with raw integers and other scalars. The method bindParam() needs explicitly a parameter to be bound to and from what i see you doing you don’t need to use it.