Gary wrote:
> Reading a book on php/mysql (Head First) and the following code is not 
> working, athough I am pretty sure I have it as they say to. Trying to kill a 
> sendmail script if I forget to enter a subject or text in body of email.  I 
> am getting the echo, but it is still sending the emails out.
> 
> What am I missing here?
> 
> Thanks.
> 
> Gary
> 
> <?php
>    $from = em...@email.com';
>    $subject =$_POST['subject'];
>    $text =$_POST['body_of_mail'];
> 
>    if(empty($subject)) {
>    if(empty($body_of_mail)){
> 
>    echo 'You forgot to enter a subject and or text in the body! <br />';
> 
> 
>    $dbc = mysqli_connect(hostet',UN,'PW','DB')
>   or die('Error connecting to MySQL server');
> 
>    $query = "SELECT * FROM table";
>    $result = mysqli_query($dbc, $query)
>     or die('Error querying database.');
> 
> 
> 
>     while ($row = mysqli_fetch_array($result)) {
>     $to = $row['email'];
>    $first_name = $row['first_name'];
>      $last_name = $row['last_name'];
>      $msg = "Dear $first_name $last_name,\n$text";
>      mail($to, $subject, $msg, 'From:' . $from);
>      echo 'Email sent to: ' . $to . '<br />';
>   }
>     $msg ="Dear $first_name.' '.$last_name\n $text";
>     mail($to,$subject,$msg,$from);
>     echo "Email sent to: $to <br /> ";
> 
>    mysqli_close($dbc);
> 
>        }
>  }
> 
>    ?> 
> 
> 

Well, first, it shouldn't do anything because you have parse errors that
should stop the script.

On your very first line you haven't surround the email in quotes (only
one quote).  Then later in the line $dbc =
mysqli_connect(hostet',UN,'PW','DB') or die('Error connecting to MySQL
server'); you're missing a quote before hostet.

Second, you look for empty vars and if they are empty then you echo that
they forgot to enter them but go on to send the emails anyway.

Then you loop through a database result and send to everyone and then
you send another one to the last person you just sent to.

Try this:

<?php

   $from = 'em...@email.com';
   $subject =$_POST['subject'];
   $text =$_POST['body_of_mail'];

   if(empty($subject) || empty($body_of_mail)) {
                die 'You forgot to enter a subject and or text in the body! <br
/>Click the back button<br />';
        } else {
                $dbc = mysqli_connect('hostet',UN,'PW','DB') or die('Error 
connecting
to MySQL server');
                
                $query = "SELECT * FROM table";
                $result = mysqli_query($dbc, $query) or die('Error querying 
database.');
                                
                while ($row = mysqli_fetch_array($result)) {
                        $to = $row['email'];
                        $first_name = $row['first_name'];
                        $last_name = $row['last_name'];
                        $msg = "Dear $first_name $last_name,\n$text";
                        mail($to, $subject, $msg, 'From:' . $from);
                        echo 'Email sent to: ' . $to . '<br />';
                }               
                mysqli_close($dbc);
        }

?>

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to