Solved

PHP Code - Where is my mistake?

Posted on 2011-09-30
15
282 Views
Last Modified: 2012-05-12
The attached code is supposed to look at the eval_due_date in a mysql database & if the eval_due_date is within 60 days of todays date, it is supposed to send an email to myemail@gmail.org

When I run it it acts like it works & I get the attachment in the browser but I get no email??? Thanks for any help provided.
<?php
include('connection.php'); 

$number_of_days_before = 60;
$email = "myemail@gmail.org";
$reminder_details = "";
$todays_date = date( "Ymd" );
echo $todays_date;
$year = substr($todays_date, 0, 4);
$month = substr($todays_date, 4, 2);
$date = substr($todays_date, 6, 2);
$trigger_date = date("Ymd", mktime(0,0,0,$month, $date+$number_of_days_before,$year));
echo $trigger_date;

$result = mysql_query( "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );
$nr = mysql_num_rows( $result );

 if (mysql_errno()) {
  echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>";

$reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
$reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}

if( !empty( $nr ) )
{

$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

}
?>

Open in new window

attatch.JPG
0
Comment
Question by:wantabe2
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
Comment Utility
I don't see an obvious problem.  A good next step is to print debugging information.

if( !empty( $nr ) )
{

echo "attempting to send message<br />\n";

$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain";
$sent = mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

var_dump ( $sent ); echo "<br />\n";
var_dump ( $email ); echo "<br />\n";
var_dump ( $reminder_details); echo "<br />\n";
var_dump ( $mailheader); echo "<br />\n";

}

Open in new window


Caveat: I didn't test my additions.

If the problem becomes obvious after that, then fix it.  If not, copy/paste the relevant output here.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
Comment Utility
You also might want to check your error_log file (if you have access to it) for recent error messages.
0
 
LVL 10

Expert Comment

by:stu215
Comment Utility
Have you tried sending a basic mail to see if you can send mail at all from the server?
<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('yourEmail@example.com', 'My Subject', $message);
?> 

Open in new window


Did you try checking your mailError.log file to see if maybe the issue is not with the script?
http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-03.html

You'll find your error logs for PHP and Perl mail in your tmp subdirectory, in a log file called mailError.log. Messages will be saved to this file automatically whenever an error occurs.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
try without double quotes (they are unneeded)

mail($email, "Evaluation(s) due within 60 days", $reminder_details, $mailheader);
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
okay....it has started emailing me now when I run the script. There must have been something wrong with the mail server (backlogged or something) I got numerous emails. okay, now that I'm getting the emails I get the subject line I have in the code but no results int eh body of the email....
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 250 total points
Comment Utility
$result = mysql_query( "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );
$nr = mysql_num_rows( $result );

 if (mysql_errno()) {
  echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>";

while ($row = mysql_fecth_array($result)){
  $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
  $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}
}

Cheers
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
marqusG
still the same thing..I get an email but with no results. There is data within the date range that should be sent via the email but it is not getting put inthe body of the message.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
To learn the correct ways of handling DATETIME values in PHP and MySQL you might want to read over this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Try this and see if it makes sense to you.  I am assuming that the column named eval_due_date is of DATETIME data type, right?  Sorry I cannot test this because I do not have your data base, but hopefully it will work or nearly work.  At least you will be able to see the query string, and know whether the query worked or not!
<?php
include('connection.php'); 

$number_of_days_before = 60;
$email                 = "myemail@gmail.org";
$reminder_details      = NULL;
$todays_date           = date("c");
$trigger_date          = date("c", strtotime($todays_date . ' + ' .$number_of_days_before . ' DAYS'));

$query  = "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date LIMIT 1";
$result = mysql_query($query);
if (!$result) die("QUERY FAILED: $query<br/>" . mysql_error() );
$row = mysql_fetch_assoc($result);
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}
else
{
    die("NOTHING FOUND BY QUERY $query");
}
echo "SUCCESS: $query";
$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
still nothing:

QUERY FAILED: SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN 2011-09-30T18:14:03+00:00 AND 2012-09-28T18:14:03+00:00 LIMIT 1
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':14:03+00:00 AND 2012-09-28T18:14:03+00:00 LIMIT 1' at line 1

There is data in the DB & the eval_due_date is the DATE data type. I change dit to DATETIME & ran the above code with no luck either...
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Use single quotes around the datetime strings.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
But I am wondering... Did you run exactly the script I posted, or did you make some other changes?  Here is the script with the quotes in the right places in the query.  Sorry I cannot test any of this because I do not have your data base, so you will have to do the testing for us, and post the results back here.
<?php
include('connection.php'); 

$number_of_days_before = 60;
$email                 = "myemail@gmail.org";
$reminder_details      = NULL;
$todays_date           = date("c");
$trigger_date          = date("c", strtotime($todays_date . ' + ' . $number_of_days_before . ' DAYS'));

$query  = "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN '$todays_date' AND '$trigger_date' LIMIT 1";
$result = mysql_query($query);
if (!$result) die("QUERY FAILED: $query<br/>" . mysql_error() );
$row = mysql_fetch_assoc($result);
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}
else
{
    die("NOTHING FOUND BY QUERY $query");
}
echo "SUCCESS: $query";
$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
Ray,
Thanks for your help. Between my code & what you posted I was able to get it working 90%. The code below is only returning i result in the email I recieve....I know I probably don't have a ; or () somewhere but just can't seem to find it. Any suggestions before I award the points? Thanks again for your assistance.
<?php
include('connection.php'); 

$number_of_days_before = 364;
$email = "myemail@yahoo.com";
$reminder_details = "";
$todays_date = date( "Ymd" );
echo $todays_date;
$year = substr($todays_date, 0, 4);
$month = substr($todays_date, 4, 2);
$date = substr($todays_date, 6, 2);
$trigger_date = date("Ymd", mktime(0,0,0,$month, $date+$number_of_days_before,$year));
echo $trigger_date;

$result = mysql_query( "SELECT hrid, f_name, l_name, eval_due_date FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );

$row = mysql_fetch_assoc($result);
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n";
}

$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
oh, one more thing to add...the one result I get back in the email is not the one you'd think it would be. It skips October & returns an entry for December... hth
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
I was able to get this working by using the code posted by ray & by adding the while( $row = mysql_fetch_array( $result ) ) to the code.
<?php
include('connection.php'); // DB connection
// Constants
$number_of_days_before = 60;
$email = "email@yahoo.com";
$reminder_details = "";
$todays_date = date( "Ymd" );
echo $todays_date;
$year = substr($todays_date, 0, 4);
$month = substr($todays_date, 4, 2);
$date = substr($todays_date, 6, 2);
$trigger_date = date("Ymd", mktime(0,0,0,$month, $date+$number_of_days_before,$year));
echo $trigger_date;

$result = mysql_query( "SELECT f_name, l_name, eval_due_date FROM hr_info WHERE supervisor = 'Jane Doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );
$nr = mysql_num_rows( $result );

$row = mysql_fetch_assoc($result);
while( $row = mysql_fetch_array( $result ) )
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n";

}
$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now