執行時,它總是聲明「此時停車托架不可用」,即使數據庫中有其他說明。我究竟做錯了什麼?可能還有其他錯誤,但我似乎無法通過這一個。我似乎無法找到的PHP/SQL錯誤
$db = connect();
try {
$db->beginTransaction();
if(isWeekend($bookingDate)){
$stmt = $db->prepare('SELECT bayid FROM parkbay p WHERE p.bayid=:bayID AND EXISTS (SELECT * FROM parkbay p WHERE p.bayid=:bayID AND(:parktime BETWEEN avail_wend_start AND avail_wend_end) AND (:parktime+:duration BETWEEN avail_wend_start AND avail_wend_end))');
} else {
$stmt = $db->prepare('SELECT bayid FROM parkbay p WHERE p.bayid=:bayID AND EXISTS (SELECT * FROM parkbay p WHERE p.bayid=:bayID AND(:parktime BETWEEN avail_wk_start AND avail_wk_end) AND (:parktime+:duration BETWEEN avail_wk_start AND avail_wk_end))');
}
$stmt->bindValue(':bayID', $bayID, PDO::PARAM_INT);
$stmt->bindValue(':parktime', $bookingHour, PDO::PARAM_INT);
$stmt->bindValue(':duration', $duration, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch();
$stmt->closeCursor();
if (!empty($result)) {
$stmt2 = $db->prepare("SELECT * FROM booking b JOIN parkbay p ON (p.bayid=b.bayid) WHERE b.bayid=:bayID2 AND b.bookingdate=:bookingDate2 AND (:parktime2 BETWEEN b.bookinghour AND b.bookinghour + b.duration) AND (:parktime2 + :duration2 BETWEEN b.bookinghour AND b.bookinghour + b.duration)");
$stmt2->bindValue(':bayID2', $bayID, PDO::PARAM_INT);
$stmt2->bindValue(':bookingDate2', $bookingDate, PDO::PARAM_STR);
$stmt2->bindValue(':parktime2', $bookingHour, PDO::PARAM_INT);
$stmt2->bindValue(':duration2', $duration, PDO::PARAM_INT);
$stmt2->execute();
$result = $stmt2->fetchAll();
$stmt2->closeCursor();
if (empty($result)) {
$stmt3 = $db->prepare("SELECT c.name, t.width, t.height, t.length FROM cartype t JOIN car c ON (c.make = t.make AND c.model = t.model) WHERE c.name=:carname AND EXISTS (
SELECT p.site, p.length, p.width, p.height FROM parkbay p WHERE p.bayid=:bayID3 AND t.width<=p.width AND t.length<=p.length AND t.height<=p.height)");
$stmt3->bindValue(':bayID3', $bayID, PDO::PARAM_INT);
$stmt3->bindValue(':carname', $car, PDO::PARAM_STR);
$stmt3->execute();
$result = $stmt3->fetch();
$stmt3->closeCursor();
if (!empty($result)) {
$stmt4 = $db->prepare('INSERT INTO booking (bookingid, bayid, bookingdate, bookinghour, duration, memberno, car) VAlUES (DEFAULT, :bayIDBooking, :bookingDateBooking, :bookingHourBooking, :durationBooking, :memberNoBooking, :carBooking) RETURNING bookingid');
$stmt4->bindValue(':bayIDBooking',$bayID,PDO::PARAM_INT);
$stmt4->bindValue(':bookingDateBooking',$bookingDate,PDO::PARAM_STR);
$stmt4->bindValue(':bookingHourBooking',$bookingHour,PDO::PARAM_INT);
$stmt4->bindValue(':durationBooking',$duration,PDO::PARAM_INT);
$stmt4->bindValue(':memberNoBooking',$memberNo,PDO::PARAM_INT);
$stmt4->bindValue(':carBooking',$car,PDO::PARAM_STR);
$stmt4->execute();
$bookingID = $stmt4->fetchColumn();
$stmt4->closeCursor();
if ($stmt->execute()) {
$db->commit();
$stmt5 = $db->prepare('SELECT b.*, (p.hourly_rate * b.duration) as cost FROM booking b JOIN member m ON (b.memberno = m.memberno) JOIN membershipplan p ON (m.plan = p.title) WHERE b.bookingid=:bookingID');
$stmt5->bindValue(':bookingID',$bookingID,PDO::PARAM_INT);
$stmt5->execute();
if ($stmt->execute()) {
$results = $stmt5->fetch();
$stmt5->closeCursor();
$results['status']='success';
return $results;
}else{ //STMT 5 - confirm Booking Insert and Cost - Fail
$db->rollback();
$results['status']=' Error making booking and getting cost. Check that membership plan is set.';
return $results;
}
}else{ //STMT 4 - Insert Booking Data - Fail
$db->rollback();
$results['status']='Error making booking.';
return $results;
}
} else { //STMT 3 - Check Car Dimensions - Fail
$db->rollback();
$results['status']='Your car is too big for this parking bay.';
return $results;
}
} else { //STMT 2 - Check Existing Bookings - Fail
$db->rollback();
$results['status']='A booking already exists at this time.';
return $results;
}
} else { //STMT 1 - Check Parking Availability - Fail
$db->rollback();
$results['status']='This parking bay is not available at this time.';
return $results;
}
} catch (PDOException $e) {
print "Error submitting query to databse: " . $e->getMessage();
;
}
}
其中:
function isWeekend($bookingDate) {
return (date('N', strtotime($bookingDate)) >= 6);
你的代碼太長了,如果你想得到一些幫助,你應該刪除所有不相關的部分。如果你希望你的代碼可以維護,你應該創建幾個函數來完成你想要實現的功能 – n00dl3