gpt4 book ai didi

php - 打开电子邮件跟踪而不更新它两次

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:22 24 4
gpt4 key购买 nike

我的 PHP 代码有问题,因此需要一些帮助。当我收到电子邮件并打开它时,它会更新数据库中的值,因为它会更新两次。示例:当我打开电子邮件时,它应该像 1 这样更新值,但它会更新 2 值而不是 1。

这是 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);
?>

我发现的问题与这一行有关:

else if ($opened == 1)

我已经添加了 $open_again = 'opened' 看看它是否有助于阻止它,但它没有帮助。我也试过这个,但没有任何区别。

$tracking_sql = mysqli_fetch_assoc(mysqli_query($link, "SELECT username, subject, campaign, newsletter_type, opened FROM tracking2 WHERE id = '$id' LIMIT 1"));

我无法解决问题,因为它在我第一次收到电子邮件后不断更新两次。当我使用 $id 变量在数据库中搜索 id 以在与 id 相同的行中更新它时,我没有使用循环或任何东西,因为它应该只更新一次。我认为问题与 open_mail.php 有关。

这是电子邮件底部显示的内容:

<img src=3D"http://example.com/open_mail.php?id=3D2602 " style=3D"width: 0; 
max-height:0; overflow:hidden; ">

我要实现的是当我第一次收到邮件打开它时,我想在跟踪表中搜索id并将其更新为1,所以当我再次打开邮件时,它会更新值到 2,然后是 3、4、5...等等。

你能给我举个例子吗?当我第一次收到我打开的电子邮件后,我可以使用什么方法来阻止两次更新?

最佳答案

更新语句可以引用它们自己的列,例如:

UPDATE tracking2 SET opened = opened + 1 ...

所以在上下文中:

<?php

header("Content-Type: image/jpeg");
readfile("image.jpeg");

//Connect to the database
include('config.php');

$id = $_GET['id'];
$stmt = $link->prepare('UPDATE tracking2 SET opened = opened + 1, datetime=NOW() WHERE id = ?');
$stmt->bind_param('s', $id);
$stmt->execute();

//close the connection
mysqli_close($link);

关于php - 打开电子邮件跟踪而不更新它两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54816764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com