I have been grafting hard with my PHP exercises and what not. Been going really well... But got stuck on a little hitch. This should work... Why doesn't it???
PHP Code:
//variables
$from = 'DO NOT REPLY - Elvis Online Store';
$subject = $_POST['subject'];
$body = $_POST['elvismail'];
//validation for fields not to be empty
if((!empty($subject)) && if(!empty($body)))
{
//connect to db
$dbc = mysqli_connect('localhost','root','PASSWORD') or die('Error connecting to MySQL');
//choose the database
mysqli_select_db($dbc, 'elvis_store');
mail('$email','$subject','$msg','$from');
echo "email has been sent to: $email<br />";
}
//close connection
mysqli_close($dbc);
}
else
{
echo "You forgot the body and/or subject";
}
The main point of this chapter is to create a "sticky" form, thus teaching if's and else's and operators. So, the end result should either be, a list of successfully sent emails, or a empty form field notification.
PS: This does go into more detail in the book. This is just where I am stuck atm.
Be careful while using select_db after the connect
//variables
$from = 'DO NOT REPLY - Elvis Online Store';
$subject = $_POST['subject'];
$body = $_POST['elvismail'];
//validation for fields not to be empty
if((!empty($subject)) && if(!empty($body)))
{
//connect to db
$dbc = mysqli_connect('localhost','root','PASSWORD') or die('Error connecting to MySQL');
//choose the database
//HERE WAS THE MISTAKE !!!
mysqli_select_db('elvis_store',$dbc);// you had reversed the arguments !!
//or just do it mysqli_select_db('elvis_store') it will take the last successful //connection by default
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result))
{
$firstName = $row['first_name'];
$lastName = $row['last_name'];
$email = $row['email'];
$msg = "Dear $firstName $lastName, \n $body";
mail('$email','$subject','$msg','$from');
echo "email has been sent to: $email<br />";
}
//close connection
mysqli_close($dbc);
}
else
{
echo "You forgot the body and/or subject";
}
Umm ... I think your arguments were actually in the appropriate order. They're "backwards" from the "pre-improved" PHP mysql functions. Your problem may actually be here:
PHP Code:
$from = 'DO NOT REPLY - Elvis Online Store'; // ... snip ... mail('$email','$subject','$msg','$from');
I'm pretty sure the 4th parameter needs properly formatted mail headers. "DO NOT REPLY - Elvis Online Store" will probably not suffice. See the examples on the mail() doc page.
Another thing to note - you are telling the user the mail was sent but you don't actually know this because you don't check the return from the mail() function. As svidgen points out, the mail would likely not have sent but your script would still inform the user that it had.
As svidgen points out, the mail would likely not have sent but your script would still inform the user that it had.
I'm embarrassed. I really didn't even catch the syntax issues--that's what I meant to say in my last post. But, in my morning half-awake state I couldn't even communicate that properly.
Sorry about that ...
I'm glad some other folks were watching this thread. I would have possibly helped solved the smallest part of the problem, only to have the remaining, bigger, and more obvious issue(s) hanging out there ...
hahaha... U guys crack me up. I got the whole thind running awhile ago. Can't remember how. What I have resorted to doing is using comments, for scripts that don't work. I will comment out everything that could potentially be a problem area. The uncomment stage for stage, until a problem occurs and then I can pin point it... Thanks alot for all the help and input.
Question: How would I confirm that the email was sent???
Mail yourself then check your inbox :P You can check the return of the mail() function, but that only tells you if the mail was accepted for delivery, not that it was actually sent and received properly.
Bookmarks