Re: Studying IF statements

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shawn

Thanks for the help, and your right, I peeked ahead and the lesson goes in 
that direction.

Thanks again

Gary

"Shawn McKenzie" <nospam@xxxxxxxxxxxxx> wrote in message 
news:CF.57.22219.F310FB94@xxxxxxxxxxxxxxx
> Shawn McKenzie wrote:
>> Gary wrote:
>>> Shawn
>>>
>>> Thanks for your reply.  Some of what you are saying is a little ahead of 
>>> my
>>> lessons, but let me address as best I can. The script worked fine in the
>>> previous lesson where I was to send emails from my DB, this lesson is to
>>> kill the email from being sent if empty.
>>>
>>>> On your very first line you haven't surround the email in quotes (only
>>>> one quote).
>>> That worked fine with the single quotes and is listed in the book that 
>>> way.
>>>
>>> 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.
>>> The single quote was deleted when I was sanitizing the code to post 
>>> here,
>>> but it is in the code...sorry.
>>>
>>> So the jist of what you are saying is to add "die" after the if
>>> statements...but they are not in the book
>>>
>>> *****
>>>
>>> I tried your code and it was not working either, am getting a parse 
>>> error
>>>
>>> Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in
>>> on line 109
>>>  Line 109         die 'You forgot to enter a subject and or text in the
>>> body! <br/>Click the back button<br />';
>>>
>>> "Shawn McKenzie" <nospam@xxxxxxxxxxxxx> wrote in message
>>> news:B8.22.22219.724FEB94@xxxxxxxxxxxxxxx
>>>> 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 = emial@xxxxxxxxx';
>>>>>    $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 = 'emial@xxxxxxxxx';
>>>>   $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
>>>
>>
>> So goes the typing code in to email :-)
>>
>> Try replacing the die line with:
>>
>> die('You forgot to enter a subject and or text in the body!<br />Click
>> the back button.<br />');
>>
>
> This is just an example using the code that you posted as a learning
> exercise.
>
> Your original IF said:  if $subject is empty, then evaluate the next if
> $body_of_mail is empty, if so, then echo 'You forgot to enter a subject
> and or text in the body! <br />' THEN execute the rest of the code which
> returns records from the db and then loops through them and send an
> email to the addresses from the db query.
>
> I just added a die() which kills the script execution IF the fields are
> empty and an ELSE, which is evaluated if the fields are NOT empty, that
> does the db query and email.
>
> In a real world example, instead of die() you might reload the form and
> display a message, so that the user can correct mistakes and resubmit.
>
>
> -- 
> Thanks!
> -Shawn
> http://www.spidean.com 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux