0

我正在制作一个重复事件系统,并且必须对检查进行硬编码以查看事件是否已通过以创建新事件。(不,我无法进行 cron 作业,因为系统基础是 Windows,不,我无权访问任务调度程序。)

这是我到目前为止的代码:

<?php
require('common.php');
$query = "SELECT 
             *
          FROM 
             DD_events
      ";
try{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
$chk = $stmt->fetchall();
//print_r($chk);

foreach ($chk as $chks) {
if (time() > $chks['event_date']) {
//Add 1 day to event time
$time = strtotime($chks['event_date']);
$newTime = $time + 86400; // 24 * 60 * 60

// Create query to make the next event
    $query = "INSERT INTO
            DD_events (event_name, event_date, initiator, min_lvl, max_level)
              VALUES 
            ({$chks['event_name']}, {$newTime}, 0, {$chks['min_lvl']}, {$chks['max_level']})
      ";
    try
    {
        // These two statements run the query against your database table.
        $stmt = $db->prepare($query);
        $stmt->execute();
    }
    catch(PDOException $ex)
    {
        // Note: On a production website, you should not output $ex->getMessage().
        // It may provide an attacker with helpful information about your code. 
        die("Failed to run query: " . $ex->getMessage());
    }
}
}

它返回此错误:

Failed to run query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 200)' at line 4

任何人都可以发现我正在犯的错误,因为我在过去 30 分钟内一直在查看它并且看不到它。

4

3 回答 3

2

您需要引用变量,但这会使您的查询容易受到 sql 注入的影响,因此正确的解决方案是使用绑定变量:

$query = "INSERT INTO
        DD_events (event_name, event_date, initiator, min_lvl, max_level)
          VALUES 
        (:event_name, :event_date, 0, :min_lvl, :max_level)
  ";
try
{
    // These two statements run the query against your database table.
    $stmt = $db->prepare($query);
    $stmt->execute(array(':event_name' => $chks['event_name'],
                         ':event_date' => $newTime,
                         ':min_lvl' => $chks['min_lvl'],
                         ':max_level' => $chks['max_level']));
}
于 2012-12-31T14:47:16.170 回答
0

假设您需要替换event_name为. 根据数据类型,这也可能适用于VARCHAR{$chks['event_name']}'{$chks['event_name']}'event_date

于 2012-12-31T14:46:42.103 回答
0

也许更简单的方法来做同样的事情:

INSERT INTO DD_events
(event_name, event_date, initiator, min_lvl, max_level)
SELECT event_name, DATE_ADD(event_date, INTERVAL 1 DAY), 0, min_lvl, max_lvl
FROM DD_events

这将在一天后为每个现有事件创建一个新事件(这是你所说的代码应该做的),并且基本上不需要你的整个脚本

于 2012-12-31T14:47:31.010 回答