SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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\n"
    . "Reply-To:ana_wanie@rocketmail.com"
    . "\r\n"
    . "X-Mailer: PHP/" . phpversion();
    if(mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully!";
    } else {
    echo "<script>alert('Failure: Email was not sent!')</script>";
    }
    }
    ?>

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    $record is a mysql result. You need to get the value out of it first.

    (hint: mysql_fetch_row 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 mysqli or PDO
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My data in a table that need to trigger with email alert.

    Item Total InStock
    Book 108 10
    Jurnal 110 3

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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\n"
    . "Reply-To:ana_wanie@rocketmail.com"
    . "\r\n"
    . "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.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You have this:
    PHP Code:
    //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 ) { 
    You just have not connected $data to $total.

    If you write an extra line:

    PHP Code:

     var_dump
    ($data

    //coding check whether stock less than 3 (2 or less)
    if( $total ) {
    // 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.

    PHP Code:
    // 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;




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •