Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

PHP & MySQL Help Needed

The code below works perfect. The only thing I need edited is, after the code runs, is there a way for me to update the email_sent field to a 0

The database name is district_db & the table is named assignments & the field name is email_sent

Thanks

<?php 

// ALWAYS SHOW ALL OF THE ERROR MESSAGES WHEN WE ARE DEVELOPING OUR CODE
error_reporting(E_ALL);

// BRING IN OUR COMMON INITIALIZATION SCRIPT
require_once('connection_test.php');

// WHO WILL WE SEND THE EMAIL TO?
$email = "myemail@yahoo.com";

// THIS WILL CONTAIN THE TEXT FOR THE EMAIL MESSAGE
$reminder_details = "";

$sql = "SELECT name, additional_notes FROM assignments WHERE active = '1' AND email_sent = '1'";

// RUN THE QUERY AND SHOW DIAGNOSTICS IF THE QUERY FAILS
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, SHOW THE ERROR
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS

// GET THE NUMBER OF ROWS
$nr = mysql_num_rows($res);

// SHOW THE NUMBER OF ROWS
echo "<br/>QUERY FOUND $nr ROWS";

// USE AN ITERATOR TO ACCESS EACH ROW OF THE RESULTS SET
while ($row = mysql_fetch_assoc($res))
{
    // ADD NAMES AND DATE TO THE EMAIL TEXT
	$reminder_details .= "Name: " . $row["name"] . " \n";
    $reminder_details .= "Instructions/Notes: " . $row["additional_notes"] . "\n\n";
}

// CREATE THE ADDITIONAL HEADERS FOR THE EMAIL
$mailheader = "From: The System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";

// SEND THE MAIL OR SHOW A DIAGNOSTIC MESSAGE
if (!mail("$email", "You have a new assignment", "$reminder_details", "$mailheader")) echo "<br/>MAIL FAIL";

// SHOW WHAT WE SENT
echo "<pre>";
var_dump($reminder_details);

Open in new window

SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sooner or later you're going to have a problem with this script, because PHP is doing away with MySQL support, so you will need a different data base extension.  This article explains why and what you must do to keep the script running in the future.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

As written, it appears that the script will only send one email message.  Is that right?  Or do you expect more than one row in the results set from the query on line 18?  If you get several rows, we need to reorganize the script a little bit so each of the rows is updated (I think).  Let us know, thanks. ~Ray
Avatar of wantabe2

ASKER

That's great & it works just like I need it to thanks! The only thing now is, & I don't know why, if the email_sent field is a 0 it will send a blank email. If the email_sent field is a 1 it will send that data in the body of the email like I need it to. Here's the final code:

<?php 

// ALWAYS SHOW ALL OF THE ERROR MESSAGES WHEN WE ARE DEVELOPING OUR CODE
error_reporting(E_ALL);

// BRING IN OUR COMMON INITIALIZATION SCRIPT
require_once('connection_test.php');

// WHO WILL WE SEND THE EMAIL TO?
$email = "myemail@yahoo.com";

// THIS WILL CONTAIN THE TEXT FOR THE EMAIL MESSAGE
$reminder_details = "";

$sql = "SELECT name, additional_notes, clerk FROM assignments WHERE active = '1' AND email_sent = '1'";

// RUN THE QUERY AND SHOW DIAGNOSTICS IF THE QUERY FAILS
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, SHOW THE ERROR
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}

// GET THE NUMBER OF ROWS
$nr = mysql_num_rows($res);

// SHOW THE NUMBER OF ROWS
echo "<br/>QUERY FOUND $nr ROWS";

// USE AN ITERATOR TO ACCESS EACH ROW OF THE RESULTS SET
while ($row = mysql_fetch_assoc($res))
{
    // ADD NAMES AND DATE TO THE EMAIL TEXT
	$reminder_details .= "Name: " . $row["name"] . " \n";
    $reminder_details .= "Instructions/Notes: " . $row["additional_notes"] . "\n";
	$reminder_details .= "Assigned Clerk: " . $row["clerk"] . " \n \n";
}

// CREATE THE ADDITIONAL HEADERS FOR THE EMAIL
$mailheader = "From: The System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";

// SEND THE MAIL OR SHOW A DIAGNOSTIC MESSAGE
//if (!mail("$email", "You have a new assignment", "$reminder_details", "$mailheader")) echo "<br/>MAIL FAIL";

if (!mail("$email", "New assignment(s)", "$reminder_details", "$mailheader")) {
   echo "<br/>MAIL FAIL";
} else {
   //mail sent so lets update the database
   $sql = "UPDATE assignments SET email_sent = '0' WHERE active = '1' AND email_sent = '1'";
   mysql_query($sql);
}

// SHOW WHAT WE SENT
echo "<pre>";
var_dump($reminder_details);

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Warning: Untested code.
<?php 
error_reporting(E_ALL);

// BRING IN OUR COMMON INITIALIZATION SCRIPT
require_once('connection_test.php');

// WHO WILL WE SEND THE EMAIL TO?
$email = "myemail@yahoo.com";

// THIS WILL CONTAIN THE TEXT FOR THE EMAIL MESSAGE
$reminder_details = "";

$sql = "SELECT name, additional_notes, clerk FROM assignments WHERE active = '1' AND email_sent = '1'";

// RUN THE QUERY AND SHOW DIAGNOSTICS IF THE QUERY FAILS
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, SHOW THE ERROR
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}

// GET THE NUMBER OF ROWS
$nr = mysql_num_rows($res);

// SHOW THE NUMBER OF ROWS
echo "<br/>QUERY FOUND $nr ROWS";

// IF THERE ARE ANY ROWS
if ($nr)
{
    // USE AN ITERATOR TO ACCESS EACH ROW OF THE RESULTS SET
    while ($row = mysql_fetch_assoc($res))
    {
        // ADD NAMES AND DATE TO THE EMAIL TEXT
        $reminder_details .= "Name: " . $row["name"] . " \n";
        $reminder_details .= "Instructions/Notes: " . $row["additional_notes"] . "\n";
        $reminder_details .= "Assigned Clerk: " . $row["clerk"] . " \n \n";
    }

    // CREATE THE ADDITIONAL HEADERS FOR THE EMAIL
    $mailheader = "From: The System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";

    // SEND THE MAIL OR SHOW A DIAGNOSTIC MESSAGE
    if (!mail("$email", "New assignment(s)", "$reminder_details", "$mailheader")) {
       echo "<br/>MAIL FAIL";
    } else {
       //mail sent so lets update the database
       $sql = "UPDATE assignments SET email_sent = '0' WHERE active = '1' AND email_sent = '1'";
       mysql_query($sql);
    }
}
else
{
    echo "NO EMAILS SENT";
}

Open in new window

THANKS!!