I need some help with my PHP as I have got a problem with my code. When I receive the email and when I open it, it will update the value in the database as it will update like twice. Example: When I open the email, it should update the value like 1 but it will update the 2 value instead of 1.
Here is the open_mail.php:
<?php
header("Content-Type: image/jpeg");
readfile("image.jpeg");
//Connect to the database
include('config.php');
$id = $_GET['id'];
$tracking_sql = mysqli_fetch_assoc(mysqli_query($link, "SELECT username, subject, campaign, newsletter_type, opened FROM tracking2 WHERE id = '$id'"));
$param_username = $tracking_sql['username'];
$subject = $tracking_sql['subject'];
$campaign = $tracking_sql['campaign'];
$newsletter_type = $tracking_sql['newsletter_type'];
$opened = $tracking_sql['opened'];
$date = date('Y-m-d H:i:s');
if ($opened == 1)
{
if ($open_again == '')
{
mysqli_query($link, "UPDATE tracking2 SET opened = 2, datetime = '$date' WHERE id = '$id'");
$open_again = 'opened';
}
}
else if ($opened == 0)
{
if ($open_again == '')
{
mysqli_query($link, "UPDATE tracking2 SET opened = 1, datetime = '$date' WHERE id = '$id'");
$open_again = 'opened';
}
}
echo "<img src='http://example.com/Images/track.jpeg'>";
//close the connection
mysqli_close($link);
?>
The problem I found is something to do with this line:
else if ($opened == 1)
I have added $open_again = 'opened'
to see if it would help to block it, but it didn't help. And I have also tried this but it make no difference.
$tracking_sql = mysqli_fetch_assoc(mysqli_query($link, "SELECT username, subject, campaign, newsletter_type, opened FROM tracking2 WHERE id = '$id' LIMIT 1"));
I cant be able to solve the issue as it keep update it like twice after when I first received the email. I am not using the loop or anything when I am using the $id variable to search for the id in a database to update it in the same row as the id as it should have update it only once. The problem I believe is something have to do with open_mail.php.
Here is what it show on the bottom of the email:
<img src=3D"http://example.com/open_mail.php?id=3D2602 " style=3D"width: 0;
max-height:0; overflow:hidden; ">
What I want to achieve is when I first receive the email and I open it, I want to search for the id in the tracking table and update it to 1, so when I open the email again, it will update the value to 2, then 3, 4, 5...etc.
Can you please show me an example what is the best way I could use to block from updating like twice after when I first receive the email that I open so ?