You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP5.3环境下插入数据加当前时间及脚本报错求助

Hey there! Let's work through your PHP 5.3 problem—both fixing the datetime insertion for your database table and sorting out those error log entries you're seeing.

1. Adding Current Datetime to Your Insert Query

You've got two solid options here, depending on whether you want the database or PHP to handle the timestamp:

Option 1: Let the Database Do the Heavy Lifting

If your download_date column is a DATETIME or TIMESTAMP type, the simplest fix is to use your database's built-in function (like MySQL's NOW()) instead of generating the time in PHP. This avoids timezone mismatches and cuts down on code:

$sql = "INSERT INTO plugin_reward_ppd_detail 
        (reward_user_id, download_ip, file_id, download_country_group_id, download_date, reward_amount, status) 
        VALUES (:reward_user_id, :download_ip, :file_id, :download_country_group_id, NOW(), :reward_amount, 'pending')";

With this change, you can remove the download_date entry from your $vals array entirely—no need to pass it in anymore.

Option 2: Generate the Datetime in PHP 5.3

If you need to generate the timestamp in PHP (maybe for logging elsewhere first), PHP 5.3 supports both the classic date() function and the DateTime class (just note that some newer DateTime features aren't available in 5.3):

// Generate a MySQL-compatible datetime string (YYYY-MM-DD HH:MM:SS)
$currentDatetime = date('Y-m-d H:i:s');

// OR use DateTime (works in PHP 5.3+)
$currentDatetime = (new DateTime())->format('Y-m-d H:i:s');

// Update your values array with the new datetime
$vals = array(
    'reward_user_id' => $file->userId,
    'download_ip' => $usersIp,
    'file_id' => $file->id,
    'download_country_group_id' => $yourCountryGroupId, // Make sure this variable is defined!
    'download_date' => $currentDatetime,
    'reward_amount' => $yourRewardAmount // Don't forget this value either!
);
2. Troubleshooting Those Error Log Entries

From your truncated code, here are the most likely culprits for the errors:

  • Missing Parameters: Your $vals array cuts off mid-way—make sure download_country_group_id and reward_amount are properly assigned values. Missing named parameters will throw a PDO error (assuming you're using PDO here, which the named :xxx syntax suggests).
  • Invalid Datetime Format: If you were passing a datetime string that doesn't match what your database expects (e.g., DD/MM/YYYY instead of YYYY-MM-DD HH:MM:SS), the insert will fail and log an error.
  • PDO Error Mode Not Enabled: PHP 5.3's PDO defaults to silent errors, which means you might not get detailed logs. Flip on exception mode to see exactly what's breaking:
// Add this right after initializing your PDO connection
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

This will throw explicit exceptions that show up in your error log, making it way easier to debug.

3. Quick Pre-Flight Checks
  • Double-check that your download_date column is set to DATETIME or TIMESTAMP (not just DATE if you need time too).
  • Ensure your PHP timezone is set correctly to avoid timestamp mismatches:
// Put this at the top of your script (e.g., for Beijing time)
date_default_timezone_set('Asia/Shanghai');
  • Verify all named placeholders in your SQL have matching keys in the $vals array—no typos allowed!

内容的提问来源于stack exchange,提问作者nagy00

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:08:03