Insert records in mysql table using prepared statements

0

I attach the code that reads from a table and writes in other certain fields, the theme is that I followed the examples I saw in Stacoverflow ... and it does not work for me, although the recorded record legend appears, it does not record anything . Any suggestions?

foreach($emails as $email) {

    echo $email['mail'];

    $statement = $conexion->prepare("INSERT INTO mailing (id, mail, nombre, telefono, estado) VALUES (null, :mail, :nombre, :telefono, :estado)");

    $statement->execute(array(
    ':mail' => $email['mail'],
    ':nombre' => $email['nombre'],
    ':telefono' => '',
    ':estado' => '1'
    ));

    echo " - Grabado"."<br>";
}
    
asked by Ricardo Pastuszek 28.09.2018 в 14:33
source

1 answer

0

I usually do it this way, what you do is capture the possible error, since that echo grabo always executed there was an error or not, since there is nothing that conditions, it is simply the flow of execution.

<?php 



    echo $email['mail'];

    $email=$email['mail'];
    $nombre=$email['nombre'];
    $telefono='';
    $estado=1;


    $statement = $conexion->prepare("INSERT INTO mailing (mail, nombre, telefono, estado) VALUES (:mail, :nombre, :telefono, :estado)");
    //pasas los parametros de forma indidual
    $statement->bindParam(':mail', $email);
    $statement->bindParam(':nombre', $email);
    $statement->bindParam(':telefono', $telefono);
    $statement->bindParam(':estado', $estado);
    //preguntas si la sentencia fue bien recibida
    if ($statement ->execute();) {
      echo " - Grabado"."<br>";
    }
    else{ //si con esto sabras que hubo un error
      echo " - Algo Fallo! Revisar la BD"."<br>";
    }

I hope you serve, you tell me how it went:

    
answered by 28.09.2018 в 15:32