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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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@domain.com, email2@domain.com','subject','body');


}




}

 

something like that.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

if($result) {

 

mail('user@domain.com','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.

Link to comment
Share on other sites

you can do this

 

mail('user@domain.com','Test Results',$body);

 

or

 

mail('user@domain.com','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.

Link to comment
Share on other sites

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

mail('user1@domain.com,user2@domain.com','Test Results',$body);

 

you can also do:

 

mail('Name1 <user1@domain.com>, Name2 <user2@domain.com>','Test Results',$body);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.