Permitir la entrada de correo electrónico y de base de datos al enviar

Hi I've had this in place for sometime and have been doing things the hard way. It simply goes to the database and I check it frequently, but as you can assume this is a terribly tedious way of doing things. Here is my current code.

<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {

    include_once "newsletter/connect_to_mysql.php";

    // Be sure to filter this data to deter SQL injection, filter before querying database
    $name = $_POST['name'];
    $email = $_POST['email'];

    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);

    if (!$email) {

        $msg_to_user = '<br /><br /><span style="font color="FF0000">Please type an email address ' . $name . '.</span>';

    } else if ($numRows > 0) {

        $msg_to_user = '<br /><br /><font color="FF0000">' . $email . ' is already in the system.</font>';

    } else {

        $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) 
                                                    VALUES('$name','$email',now() )")  or die (mysql_error());

        $msg_to_user = '<br /><br /><div style="color=#F47926;width:180px;">Thanks ' . $name . ',  expect an email shortly!</div>';
        $name = "";
        $email = "";
    }
}
?>

I'm trying to add a simple way to do send and email with the email and name to my own email address. This way I'd not have to do any frequent checks of the database itself, but rather view them as they appear in my email. I hope this makes sense.

preguntado el 25 de abril de 13 a las 02:04

Is this PHP? You should tell us instead of making us guess. -

so sorry about that John I tagged it as PHP. The first line also says php. I thought that would be enough...yes it is php with a mysql database connection. -

It was not tagged PHP. -

I'm positive it is John, but if that's enough to not help out I'm sorry. Maybe someone else could point me in the right direction. -

I tagged it PHP. Please always tag PHP questions as PHP. Tags on SO aren't just used to tell a reader what language it is. They're also used to categorize questions in various ways. One way is that it's possible to set "PHP" as a favorite tag, so that PHP questions show up highlighted. Another way is to set "PHP" as an ignored tag, so that PHP questions are never seen. -

2 Respuestas

It would be wise to do some additional checks to validate the data being passed through $_POST.

Algo como esto debería ayudarte a empezar.

$email = filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL);

Here is how you would send an email message.

$message = 'Name: ' . $_POST['name'] . ', Email: ' . $_POST['email']; 
mail('your@email.com', 'A New User Has Signed Up',  $message);

http://php.net/manual/en/function.mail.php

http://php.net/manual/en/function.filter-var.php

Respondido 25 Abr '13, 03:04

Hi Max, thats the exact code I had before. I beleive I may be having the issue with where to place it. I get a white screen with no content when I try to add it in. I put it right before the last curly bracket. - user2251674

Scratch taht. I believe I was missing a semicolon...I just got it working. Thanks a ton for the help. I'm going to look into the first issue of filtering the variable. Thanks. - user2251674

i dont really know what needs to be done, but if you are askng for improvements or suggestions...

en lugar de esto:

$msg_to_user = '<br /><br /><span style="font color="FF0000">Please type an email address ' . $name . '.</span>';

Puedes hacerlo:

$msg_to_user = "<br /><br /><span style='color:FF0000'>Please type an email address {$name}.</span>";

note: use double quotes.

Respondido 25 Abr '13, 03:04

Using single quotes is better than double quotes - Michael Sivolobov

Double quotes always make PHP to use interpolation with whole string even if it has no variables in it. Concatenation is faster than interpolation. Take an experiment. Or see answer here: stackoverflow.com/questions/13620/… - Michael Sivolobov

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.