我在执行将执行提货日期和退货日期的搜索页面时遇到问题。在数据库的car表中添加提货日期和退货日期很重要吗?或者我可以将其添加到另一个表中
Car.php
</body>
<table align='center' width='40%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<form action="search.php" method="post" enctype="multipart/form-data">
<td><input type="date" name="Pickup_date" placeholder="Pickup_date" style='height:38px' required /></td>
<td><input type="date" name="Return_date" placeholder="Return_date" style='height:38px' required /></td>
<td><input type="submit" name="submitbook" value="Search" style='background-color: blue; border: none; color: white; padding: 10px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px;'/></td>
</div>
</form>
</table>
// Attempt select query execution
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button></td>";
echo "</tr>";
Search.php
$search=$_GET['s'];
$query="select *from car where car_name like '%$search%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100' width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "</tr>";
对不起,我想问这个无数个问题。我对php语言非常陌生。如何使car.php中的“预订”按钮可以重定向到booking.php?我希望我的搜索框可以基于提货日期和退货 car.php中的日期,当它执行时,它将重定向到search.php。 我单击按钮搜索,但显示“无法执行”。
答案 0 :(得分:0)
我将为您提供当前问题的解决方案,但您必须知道您需要遵循PHP Standards Recommendations并考虑在执行数据库查询之前运行一些验证。
这是一种老式的编码样式,我强烈建议您使用新标准进行编码。请看一下使用MySQL的simple PHP 7 shopping cart和this is another example的示例。
现在,回答您的问题:
我建议您使用一个关系表来确定提货和退货日期。
uid | car_uid | pickup_date | return_date
然后使用“ car_uid”与“ cars”表创建关系。然后使用“ JOIN”进行查询。
您的表单操作将进入search.php,因此在此文件中,您需要从car.php中的表单中读取值(Pickup_date和Return_date)。
例如(在search.php中):
$pickup_date = $_GET['Pickup_date'];
$return_date = $_GET['Return_date'];
然后创建查询:
"SELECT * FROM cars WHERE pickup_date = '".$pickup_date."' AND return_date = '".$return_date."'";
我看到,对于您的“ Book”按钮,您正在调用一个名为“ booking_car()”的JavaScript函数,如果您的函数正确,则该函数应该起作用。示例:
function booking_car(car_id) {
window.open('booking.php?id=' + car_id, '_self');
}
然后您需要在“ booking.php”中获取ID
$car_id = $_GET['car_id'];
并运行查询以预订汽车。