PHP电子邮件(如果SQL条目是新的),如果更新则没有电子邮件 [英] PHP Email if SQL entry is new, no email if update

查看:69
本文介绍了PHP电子邮件(如果SQL条目是新的),如果更新则没有电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当新用户添加到数据库时,我收到一封通过PHPMailer发送出去的电子邮件.如果电子邮件(密钥)已经存在且具有ON DUPLICATE KEY UPDATE功能,则更新DB.但是,我不希望电子邮件在更新后发送,仅在新的时候发送.如何检查(使用PHP)SQL结果是更新还是插入,然后使用IF语句发送/不发送电子邮件?

I have an email that gets sent out (via PHPMailer) when a new user is added to a database. The DB is updated if the email (key) already exists with the ON DUPLICATE KEY UPDATE function. However, I don't want the email to send if it is updated, only when it's new. How can I check (with PHP) if the SQL result was an update or an insert, and then use an IF statement to send/not send the email?

$sql = "INSERT INTO premium_waitinglist (date, email, ip) VALUES ('".$serverDate."', '".$_POST['user-email']."', '".$userIP."') ON DUPLICATE KEY UPDATE waiting = 1";
if ($conn->query($sql) == true) {} else { echo "<kbd>Error: ".$conn->error."</kbd>";}
$mail = new PHPMailer;

$fromEmail = $_POST['user-email'];

$body = file_get_contents($urlBody);

$mail->isSMTP();
$mail->Host = $mailHost;
$mail->SMTPAuth = true;
$mail->Username = $mailUsername;
$mail->Password = $mailPassword;
$mail->Port = 2525;

$mail->From = $fromEmail;
$mail->FromName = $fromName;
$mail->addAddress($toEmail, $toName);

$mail->Subject = $mailSubject;

$mail->isHTML(true);

$mail->Body = $body;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

我可能会说错了,但是$ result返回1,这似乎是正确的,但是-> affected_rows为空.

EDIT 1: I may be calling this wrong, but $result is returning 1, which seems right, but the ->affected_rows is empty.

$sql = "INSERT INTO premium_waitinglist (date, email, ip) VALUES ('".$serverDate."', '".$_POST['user-email']."', '".$userIP."') ON DUPLICATE KEY UPDATE waiting = 1";
$result = $conn->query($sql);
if ($result == true) {} else { echo "<kbd>Error: ".$conn->error."</kbd>";}
echo $result;
echo $result->affected_rows;

if ($result->affected_rows == 1) {

推荐答案

根据文档:

对于INSERT ... ON DUPLICATE KEY UPDATE语句,如果将行作为新行插入,则受影响的行值为1;如果更新现有行,则受影响的行值为2.

For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value is 1 if the row is inserted as a new row and 2 if an existing row is updated.

因此,请使用: mysql_affected_rows ,或者您的未弃用/未过时的数据库库中的任何等效项选择.

So, use: mysql_affected_rows, or whatever equivalent is on your non-deprecated/non-obsolete DB library of choice.

这篇关于PHP电子邮件(如果SQL条目是新的),如果更新则没有电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆