PHP准备语句注意:只应通过引用

时间:2017-02-18 16:28:46

标签: php mysql

我第一次写准备好的陈述

此代码每隔3小时检查一次,然后将时间间隔插入db

date_default_timezone_set('Asia/Hong_Kong');
$now = new DateTime();

if ($now->format("H:i") > "22:00") {
$deadline = DateTime::createFromFormat("H:i", "22:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";

} else if ($now->format("H:i") > "19:00") {
$deadline = DateTime::createFromFormat("H:i", "19:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";

} else if ($now->format("H:i") > "16:00") {
$deadline = DateTime::createFromFormat("H:i", "16:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";

} else if ($now->format("H:i") > "13:00") {
$deadline = DateTime::createFromFormat("H:i", "13:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";

} else if ($now->format("H:i") > "10:00") {
$deadline = DateTime::createFromFormat("H:i", "10:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";

} else if ($now->format("H:i") > "07:00") {
$deadline = DateTime::createFromFormat("H:i", "07:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
}

$stmt = $conn->prepare("INSERT INTO time_in (e_id, login, late, date_in)
 VALUES (?, ?, ?,CURRENT_TIMESTAMP)");

$stmt->bind_param("sss", $e_id, $login, $diff->format('%H:%i'));

$e_id = "id is unavailable"; // changing to $_POST in the future
$login = "1";

$status = $stmt->execute();

if(!$status) {
    echo $stmt->error;    
    exit;
}
echo "success";

}

即时通讯

  

注意:只应通过引用传递变量

此行$stmt->bind_param("sss", $e_id, $login, $diff->format('%H:%i'))

代码工作得很好,但我得到了通知 请帮忙

1 个答案:

答案 0 :(得分:3)

你必须将实际变量传递给bind_param,因为$diff->format('%H:%i')不是变量而是函数的输出,它不是那样的。

您需要先将此值分配给变量,然后将其传入。即

$diffFormat = $diff->format('%H:%i');

$stmt->bind_param("sss", $e_id, $login, $diffFormat);