當來自數據庫的用戶名==用戶名和數據庫中的日期==日期時,它應該回顯錯誤消息,而是顯示成功。我嘗試使用「=」,但即使數據被假定爲正確,結果仍會回顯錯誤消息。請幫忙。爲什麼不能顯示錯誤信息?
這是我的代碼:
if(!empty($_POST['username'])){
$username=$_POST['username'];
}
else
{
$username=null;
$usererr = "";
}
if(!empty($_POST['datepicker'])){
$date = date('y-m-d', strtotime($_POST['datepicker']));
}
else
{
$date=null;
$dateerr = "";
}
$mysqli = new mysqli("", "", "", "");
$stmt = $mysqli->prepare("SELECT username, date FROM booking WHERE username='$username' and date ='$date'");
$stmt->bind_param("ss", $username, $date);
$stmt->execute();
$stmt->bind_result($cusername, $cdate);
$stmt->fetch();
if($cusername == $username && $cdate == $date){
echo"You have already book this day. Please select another day";
}
else{
echo"Success";
$chdate = $date;
}
if($username && $date && $chdate){
session_start();
$mysqli = new mysqli("", "", "", "");
stmt = $mysqli->prepare("INSERT INTO booking (, , , ,)
VALUES ('',''");
$result = $stmt->execute();
}
$stmt->close();
$mysqli->close();
你應該使用佔位符查詢而不是變量名稱。 'WHERE username =?'你實際上試圖在那裏引用變量。 –
你有沒有試過echo date和cdate?他們完全平等嗎? –
提示:不要使用「其他」來「重置」變量。首先用「空」值來定義它們。更清晰的代碼,並且不再需要其他語句。 –