get mysql data and send it by php to an email

0

my problem is this I have this code '

if (!empty($nombre) && !empty($correo) && !empty($mensaje)) {
    $nombre = trim($nombre);
    $nombre = filter_var($nombre, FILTER_SANITIZE_STRING);
    $correo = filter_var($correo, FILTER_SANITIZE_EMAIL);
    $mensaje = htmlspecialchars($mensaje);
    $mensaje = trim($mensaje);
    $mensaje = stripslashes($mensaje);

    if(!filter_var($correo, FILTER_VALIDATE_EMAIL)){
        $errores .= 'Por favor ingresa un correo valido <br />';
    }
}else{
    $errores .= 'Porfavor rellena todos los campos';
}

if(!$errores){
    try {
        $conexion = new PDO('mysql:host=localhost;dbname=restaurante','root','');
        $statement = $conexion->prepare("SELECT * FROM carrito");
        $statement->execute();
        $resultado = $statement->fetchAll();

    } catch (PDOException $e) {
        echo "Error: ".$e->getMessage();
    }
    $enviar_a = '[email protected]';
    $asunto_a = 'Asunto: Pedido desde la pagina de Restaurante vernnys';
    $mensaje_preparado = "De: $nombre \n";
    $mensaje_preparado .= "Correo: $correo \n";
    $mensaje_preparado .= "Mensaje: " . $mensaje;


    mail($enviar_a, $asunto_a, $mensaje_preparado);
    $enviado = 'true';
}

} ? > '

I do not know how to do in the part of the

  

try {} catch {}

so that I can take the data that I have in the database and connect it to a message and send them together with name, email and message my database "cart" only has 2 columns ... product and price, I just want you to send this table to my mail along with all the above, could someone tell me how to finish my code there please?!

Thanks in advance

    
asked by Jajo Go 16.11.2017 в 19:36
source

1 answer

1

You can read your results in a foreach loop, concatenating into a variable and then adding it to the end of the message.

Also, the message must be sent within try , because if there is an error, it will try to send the message, but there will be no data.

In addition, the variable $enviado should depend on what happens with mail , since mail returns false if there was an error. Setearla always to true does not correspond to reality.

Very important note: Here $row["producto"]." - ".$row["precio"] you must write exactly how your two columns are called in the database, because I have put this: fetchAll(PDO::FETCH_ASSOC) indicating that you create an associative array of column names- > value, with given data.

The code proposal is this:

if(!$errores){
    try {
            $conexion = new PDO('mysql:host=localhost;dbname=restaurante','root','');
            $statement = $conexion->prepare("SELECT * FROM carrito");
            $statement->execute();
            $resultado = $statement->fetchAll(PDO::FETCH_ASSOC);

            $strResultado="";
            foreach ($resultado as $row){
                $strResultado .= $row["producto"]." - ".$row["precio"].PHP_EOL;
            }

            $enviar_a = '[email protected]';
            $asunto_a = 'Asunto: Pedido desde la pagina de Restaurante vernnys';
            $mensaje_preparado = "De: $nombre \n";
            $mensaje_preparado .= "Correo: $correo \n";
            $mensaje_preparado .= "Mensaje: " . $mensaje.PHP_EOL.$strResultado;

            $enviado= mail($enviar_a, $asunto_a, $mensaje_preparado);

    } catch (PDOException $e) {
        echo "Error: ".$e->getMessage();
    }
    
answered by 16.11.2017 в 20:37