Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6550391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:12:37+00:00 2026-05-25T12:12:37+00:00

I have a console application that posts to a PHP page that (using Gmail

  • 0

I have a console application that posts to a PHP page that (using Gmail and PHPMailer class) emails the user.

The problem is, the user always receives the email twice!

Here is my post from C# (I pass in uid for logging purposes.)

public static void PostUpdate(string uid)
{
    WebRequest request = WebRequest.Create("http://127.0.0.1/bin/server/check_table.php");
    request.Method = "POST";
    string postData = "check_table";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteArray.Length;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    WebResponse response = request.GetResponse();
    Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : HttpWebResponse( " + ((HttpWebResponse)response).StatusDescription + " )");
    dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    // Display the content for debugging
    // Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : " + responseFromServer);
    reader.Close();
    dataStream.Close();
    response.Close();

}

My PHP file consists of

 $sql = "SELECT * FROM users as u JOIN value_store as v ON u.id = v.uid WHERE u.last_notification < DATE_SUB( NOW( ) , INTERVAL 5 MINUTE ) ";
$results = mysql_query($sql, $db_link);
while($row = mysql_fetch_array($results)) {
    $id = $row[0];
    $msg = "";
    if($row["inlet_moisture"] > $row["inlet_moisture_high_critical"]) {
        $msg = "Critical Inlet Moisture";
    } else if($row["inlet_moisture"] > $row["inlet_moisture_high_warning"]) {
        $msg = "Inlet Moisture Warning";
    }
    if($msg != "") {
        if($row["mobile_notification"] == 1) {
            if( sendMessage(($row["mobile_number"] . "@" . $row["mobile_carrier"]), $row["first_name"], $msg) ) {
                $res1 = mysql_query("UPDATE users SET last_notification = NOW() WHERE id = $id",$db_link);
                echo "SMS Sent for [$id]";  
            }
        }
        if($row["email_notification"] == 1) {
            if( sendMessage($row["email"], $row["first_name"], $msg) ) {
                $res2 = mysql_query("UPDATE users SET last_notification = NOW() WHERE id = $id",$db_link);
                echo "Email Sent for [$id]";
            }
        }
    }
    echo "Update For [$id] Complete!";
}

I have taken into consideration some debug options posted.

I have setup my console application to read the output from the WebRequest…
So I can see I am opening the posting the page, the POST is OK. I think there might be a closure issue because there are two rows in the database, and If I echo back the ID for each row I only get the last row (twice). I have tried updating my PHP while loop to output for which id(row) i’m using.

I still receive

Update For [2] Complete!
Update For [2] Complete!

instead of

Update For [1] Complete!
Update For [2] Complete!

See above for revisions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:12:37+00:00Added an answer on May 25, 2026 at 12:12 pm

    You have this code:

    if($msg != "") {
        if($row["mobile_notification"] == 1) {
            // stuff
        }
        if($row["email_notification"] == 1) {
            // stuff
        }
    

    Which is sending an email once if the mobile_notification is set to 1, and also sending an email if the email_notification is set to 1; this could be the cause. You may want to use

        else if($row["email_notification"] == 1) {
    

    to only send the email if the mobile hasn’t been sent.

    Without knowing some more of the code or data, it’s hard to say. Some things you can do to track it down further

    Put an echo in the PHP sendMessage function:

    function sendMessage($to, $toName, $body) {
        echo 'Sending mail...';
        // rest of the function here
    }
    

    and see if it’s definitely firing that twice (it should be).

    Now have a look to see what’s coming back from the database each time

    while($row = mysql_fetch_array($results)) {
        print_r($row);
        // script continues
    }
    

    It may be getting 2 rows back for each query, in which case you may look at your SQL query to see if that’s correct.

    Assuming it is, maybe the PHP file is being called twice, so put a debug in the C# file (I don’t know C# so I’ll leave the syntax to you)

    public static void PostUpdate(string uid)
    {
        // echo uid here
        // script continues
        WebRequest request = WebRequest.Create("http://127.0.0.1/bin/server/check_table.php");
        request.Method = "POST";
    

    By just printing some output, you can track where it’s going wrong, and then look at why it’s going wrong once you know that.


    I’ve just seen that you’re doing this…

    while($row = mysql_fetch_array($results)) {
    

    And then later on, in that loop, saying

     $results = mysql_query("UPDATE users...
    

    And re-defining the result set you’re already looping on. You can run a query without taking a return value;

     mysql_query("UPDATE users
    

    or just give it a different name the second time

    $results2 = mysql_query("UPDATE users...
    

    And it’ll probably work 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a console application that will be kicked off with a scheduler. If
I have a console application that require to use some code that need administrator
I have a console application that I would like to run as 'NT AUTHORITY\NetworkService',
I have a console application that contains quite a lot of threads. There are
I have a console application that should do best effort logging to a database
I have a console application that communicates with a web service. Both of them
I have a console application that is trying to load a CustomConfigurationSection from a
Scenario: I have a console application that needs to access a network share with
I have created a console application that calls a method on a webservice. I
I have a .NET console application that needs to generate some HTML files. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.