【问题标题】:Can't update SQL column with PHP无法使用 PHP 更新 SQL 列
【发布时间】:2019-10-06 00:01:18
【问题描述】:

我创建了一个由每周 CronJob 触发的 PHP 脚本。 如果上次活动的日期超过 2 周前,则某个字段将变为红色,并且一旦 CronJob 触发,将向特定人员发送一封电子邮件。

我可以获得需要邮寄的数据,而且它已经邮寄了。现在我只需要将字段mailed 更新为1

我在phpMyAdmin中尝试了SQL查询:

UPDATE klanten_potentioneel SET mailed = 1 WHERE naamklant = '$naam'

$naam 显然已更改为现有名称。此查询有效。

<?php 
$conn = new mysqli("localhost", "xxx", "xxx", "xxx");
$sql = "SELECT * FROM table WHERE mailed IS NULL GROUP BY naamklant ORDER BY id DESC ;";
                            $result = $conn->query($sql);

                            if ($result->num_rows > 0) {
                                while ($row = $result->fetch_assoc()) {


if (strtotime($row["datumlastvisit"]) < strtotime('-14 days')) {
    $mailNeeded = '1 ';
    //hier de code van mailen
                                        $to = 'fictional@mail.com';
                                        $subject = 'geen actie potentieel';
                                        $message = "Er is nog geen actie ondernomen bij $row[naamklant] sinds $row[datumlastvisit] door $row[naam]."; 
                                        $headers = 'From: noreply@mail.nl\r\n';
                                        // Sending email
                                        if(mail($to, $subject, $message,$headers)){

                                        }
                $naam = $row["naamklant"];


$sql = "UPDATE klanten_potentioneel SET mailed = 1 WHERE naamklant = '$naam'";



  }                                  
else if (strtotime($row["datumlastvisit"]) < strtotime('-7 days')) {
    $mailNeeded = '0';
} else {
    $mailNeeded = '0';
}
//to test if the code is working
echo $row["naamklant"];
echo " - ";
echo $mailNeeded;
echo "<br>";


                        }
                     }
                      else {
                                echo "There is nothing to be mailed.";
                            }
?>

数据库中的记录应该会发生变化,但事实并非如此。

【问题讨论】:

  • 您在创建更新查询后又忘记使用$conn-&gt;query($sql)
  • 抱歉,UPDATE 语句实际执行在哪里?
  • 嘿。感谢您的回复!添加 $conn->query($sql);查询工作后。谢谢
  • 您的代码易受 SQL 注入攻击。您应该使用准备好的语句。

标签: php sql cron


【解决方案1】:

一旦您完成了查询构建,您就可以执行它并获得它的结果。 在这种情况下,您在 UPDATE 查询之后没有这样做。

添加

$conn-&gt;query($sql);

之后

$sql = "UPDATE klanten_potentioneel SET mailed = 1 WHERE naamklant = '$naam'";

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多