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.
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! );
From your truncated code, here are the most likely culprits for the errors:
- Missing Parameters: Your
$valsarray cuts off mid-way—make suredownload_country_group_idandreward_amountare properly assigned values. Missing named parameters will throw a PDO error (assuming you're using PDO here, which the named:xxxsyntax suggests). - Invalid Datetime Format: If you were passing a datetime string that doesn't match what your database expects (e.g.,
DD/MM/YYYYinstead ofYYYY-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.
- Double-check that your
download_datecolumn is set toDATETIMEorTIMESTAMP(not justDATEif 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
$valsarray—no typos allowed!
内容的提问来源于stack exchange,提问作者nagy00

