我正在处理一个表单,在该表单中,我需要检查是否有一个或多个特定值/值的实例。问题是在测试时,我得到“日期已被预订”,即使我选择的值不是。
这是我的代码 -
if ($_POST) {
if (isset($_POST['proceedtopaypal'])){
$apartment = $_POST['apartment'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$address = $_POST['address'];
$mobile = $_POST['mobile'];
$pax = $_POST['pax'];
$address = $_POST['address'];
$remarks = $_POST['remarks'];
$day_from = $_POST['day_from'];
$month_from = $_POST['month_from'];
$year_from = $_POST['year_from'];
$booking_from = $year_from."-".$month_from."-".$day_from; (format- yyyy-mm-dd)
$day_to = $_POST['day_to'];
$month_to = $_POST['month_to'];
$year_to = $_POST['year_to'];
$booking_to = $year_to."-".$month_to."-".$day_to;
$no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from));
$days = floor($no_of_nights / (60*60*24));
$validdate = false;
$buttonpressed = false;
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
if (!checkdate($month_from, $day_from, $year_from)) {
echo "Check in date is invalid";
}
else if (!checkdate($month_to, $day_to, $year_to)) {
echo "Check out date is invalid";
}
else if ($booking_from > $booking_to) {
echo "Check in date is after check out date";
}
// check if all info is filled in
else if (($name == "Name") || ($surname == "surname") || ($email == "Email") || ($mobile == "mobile") || ($address == "Address")) {
echo "Please fill in the missing information";
}
else if (IsInjected($email)) {
echo "Not an email";
}
else if ($validdate == false) {
$final = true;
include 'connect.php';
$sql = "SELECT COUNT(*) FROM room_nights WHERE apartmentID= '$apartment' AND dates >= '$booking_from' AND dates < '$booking_to'";
$result = mysqli_query($conn, $sql);
$data = mysqli_num_rows($result);
$row=mysqli_fetch_row($result);
echo $row[0];
if ($data > 0) {
echo "Dates are already booked!";
}
else {
echo "Proceed";
}
}
}
}
这个问题的最佳解决方案是什么,以便我可以继续我的工作。
答案 0 :(得分:0)
$data = mysqli_num_rows($result);
没用。在查询中使用SELECT COUNT(*)
时,不应使用mysqli_num_rows
函数。你可以使用这个:
$sql = "SELECT COUNT(*) FROM room_nights WHERE apartmentID= '$apartment' AND dates >= '$booking_from' AND dates < '$booking_to'";
$result = mysqli_query($conn, $sql);
$row=mysqli_fetch_row($result);
echo $row[0];
if ($row[0] > 0) {
echo "Dates are already booked!";
}
else {
echo "Proceed";
}