简体   繁体   中英

how to send email automatically when a value is updated in mysql database

i design an application and build with phonegap all is working but when a value in my mysql database that is attached the app is updated i want an email to be sent automatically to the user. When the counter completes counting the value of "status" in runnindAds table is updated and i want to an email to be sent each time that value is updated, below is the script i wrote for the email to be sent but i don't know how to automate it, please help with what i can should or i can

<?
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";

$id=$_GET["rid"];
$status = 3;
mysql_query("update runningAds set status = 3 where id = '$id'");

// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];

sendConfirmationEmail($username, $stopTime);

header("location: ../view_running_ads.php");

function sendConfirmationEmail($username, $stopTime)
    {
        $result2 = mysql_query("select * from dapUsers where userName='$username'");
        $row = mysql_fetch_array($result2);


        $to = $row['email'];
        //$from = "admin@addirectng.com";
        $subject = 'Advert Runtime Alert';
        $message = "Dear $username,<br \>
        Your display picture advert runtime has ended at $stopTime. <br \>
        Advert Direct Team";

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: <admin@addirectng.com>' . "\r\n";

        mail($to,$subject,$message,$headers);

    }
?>

Try this:

<?php
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";

$id=$_GET["rid"];
$status = 3;

 // get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];

if (mysql_query("update runningAds set status = 3 where id = '$id'"))
{
     sendConfirmationEmail($username, $stopTime);
}





header("location: ../view_running_ads.php");

function sendConfirmationEmail($username, $stopTime)
    {
        $result2 = mysql_query("select * from dapUsers where userName='$username'");
        $row = mysql_fetch_array($result2);


        $to = $row['email'];
        //$from = "admin@addirectng.com";
        $subject = 'Advert Runtime Alert';
        $message = "Dear $username,<br \>
        Your display picture advert runtime has ended at $stopTime. <br \>
        Advert Direct Team";

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: <admin@addirectng.com>' . "\r\n";

        mail($to,$subject,$message,$headers);

    }
?>

Tell me if it works or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM