Reputation: 17667
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.
Upvotes: 0
Views: 535
Reputation: 15802
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 :)
Upvotes: 1