Jump to content

fire off email when database has entry (w/ PHP)


webguync

Recommended Posts

there is a couple ways.

 

-either when you do an update statement check to see if the value is set to one and if it is send out the email.

-set up a cron to automatically check the database after a certain period of time and send out emails for every record it finds with value = 1

you'll have to check with your web server to see if they allow for crons to be set up. it's basically a daemon that runs on your web server where you can set triggers to go off after certain time intervals (minute, hour, day, month, etc).

 

In this case, it would trigger a PHP script which would select all the rows from your db where value = 1 then send off an email to your group.

so you'd likely be doing this via a form post when you update the record.

if($_POST['submitButton']){

//validate post data here


//run update query. value and id are from your $_POST data
$sql = "UPDATE table set value = '$value' WHERE id = $id";
mysql_query($sql);

//check value to see if it meets our threshold 
if($value == 1){

	mail('[email protected], [email protected]','subject','body');


}




}

 

something like that.

I have code which created the log entry, so I should just be able to add the email part to this right?

 

<?php
//include Database Class
require_once('databaseClass.php');

$userID = $_POST['userID'];
$sect = 1;
$time = gmmktime();
$attempt = 1;

$db = new Database('localhost','username','password','DBName');

$sql = "INSERT INTO og_March2010 (user_id,section,date,attempt) VALUES ($userID,$sect,$time,$attempt)";
$result = $db->query($sql);

if($result) {
$replySQL = "SELECT log_id FROM og_March2010 WHERE user_id=$userID";
$kickback = $db->query($replySQL);
$kickback->get_rows();
$row = $kickback->fetch_assoc();
$logID = $row['log_id'];
$db->close();

echo 'triumph=sieg&logid='.$logID;
}

?>

 

anytime there is an entry I would want an email to be sent.

 

if($result) {

 

mail('[email protected]','subject','body');

 

$replySQL = "SELECT log_id FROM og_March2010 WHERE user_id=$userID";

$kickback = $db->query($replySQL);

$kickback->get_rows();

$row = $kickback->fetch_assoc();

$logID = $row['log_id'];

$db->close();

 

echo 'triumph=sieg&logid='.$logID;

}

/php]

 

you can put $userID,$sect,$time,$attempt in a string if you want to include them in the body of the email.

you can do this

 

mail('[email protected]','Test Results',$body);

 

or

 

mail('[email protected]','Test Results',"$body");

 

if you put a string in single quotes, PHP will not look for any variables inside the string. So your body would of looked like "$body" when you got the email.

yup. but keep it in the same string as it's the same argument for mail()

mail('[email protected],[email protected]','Test Results',$body);

 

you can also do:

 

mail('Name1 <[email protected]>, Name2 <[email protected]>','Test Results',$body);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.