Email Alert For Stock Less

Hi all,
Please help me on how to get email alert for admin when stock less than 3.
The admin will get automaticly email notification in his mailbox.
Anyone who know about this?

Thank you.

Literally, the solution will probably look like this:

Query the database to get the stock number

// Analyse the stock number

if( $stock_number < 3 ){

// send email

}

Now, why don’t you expand on your question and show us the code you have so far, what your db schema looks like (if indeed you are using a db) and what errors you are getting.

The first trial coding like below. No error has display but does not get email alert in mailbox.
<?php
error_reporting(E_ALL ^ E_NOTICE);
//select InStock data from table ‘accessories_update’
$record = mysql_query(“SELECT InStock FROM accessories_update”) or die(mysql_error());

//coding check whether stock less than 3 (2 or less)
if( $record< 3 ) {
$to = ‘ana_wanie@rocketmail.com’;
$subject = ‘Warning! Less stock…’;
$message = ‘Please add your stock! Your stock is already less than 3 right now…’;
$headers = ‘From: nfsmohamad@gmail.com
. "\r
"
. “Reply-To:ana_wanie@rocketmail.com
. "\r
"
. “X-Mailer: PHP/” . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo “Email sent successfully!”;
} else {
echo “<script>alert(‘Failure: Email was not sent!’)</script>”;
}
}
?>

$record is a mysql result. You need to get the value out of it first.

(hint: [FPHP]mysql_fetch_row[/FPHP] will give you an array. the 0th item in the array is your number.)

-Standard MySQL Library reminder-
the mysql_ commands are being deprecated as of PHP 5.5; consider strongly moving up to [FPHP]mysqli[/FPHP] or [FPHP]PDO[/FPHP]

My data in a table that need to trigger with email alert.

Item Total InStock
Book 108 10
Jurnal 110 3

Thanks to Cups and StarLion for your rensponse.

Did you means like this StarLion,

<?php
error_reporting(E_ALL ^ E_NOTICE);
//select all data from table ‘accessories_update’
$record = mysql_query(“SELECT InStock FROM accessories_update”) or die(mysql_error());

//select total number of row from the previous record
$total =mysql_fetch_row($record) or die(mysql_error());

//select all data form first row from the record
$data = mysql_fetch_array($record) or die(mysql_error());

//coding check whether stock less than 3 (2 or less)
if( $total < 3 ) {
$to = ‘ana_wanie@rocketmail.com’;
$subject = ‘Warning! Less stock…’;
$message = ‘Please add your stock! Your stock is already less than 3 right now…’;
$headers = ‘From: nfsmohamad@gmail.com
. "\r
"
. “Reply-To:ana_wanie@rocketmail.com
. "\r
"
. “X-Mailer: PHP/” . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo “Email sent successfully!”;
} else {
echo “<script>alert(‘Failure: Email was not sent!’)</script>”;
}
}

?>

Success get email alert in mailbox BUT for all conditions by using above coding.
When click refresh button, email has been sent and also when the stock is not less than 3.
Please help me, I’m not expert in php. A new learner.

You have this:


//select all data form first row from the record 
$data = mysql_fetch_array($record) or die(mysql_error());

//coding check whether stock less than 3 (2 or less)
if( $total < 3 ) {

You just have not connected $data to $total.

If you write an extra line:



 var_dump($data) 

//coding check whether stock less than 3 (2 or less)
if( $total < 3 ) {
// etc

you should see your InStock number, you need to check that is below 3

Show us the output of $data

Also, when testing code that is going to call mail(), just output the strings at first, then, when everything is working as it should, add your mail function back in. I find it far more comfortable to develop like that.


// comment the lines out temporarily

//if(mail($to, $subject, $message, $headers)) {
//echo "Email sent successfully!";
//} else {
//echo "<script>alert('Failure: Email was not sent!')</script>";
//}

echo 'SENDING MAIL:' . PHP_EOL ;
echo $to, $subject, $message, $headers;

}