我正在尝试创建一个搜索工具,搜索两个不同表中的2个不同列,因此如果来自hotel表的任一名称匹配或guest表中的guest_id,则输出while循环中的任何内容。但是其中一个有效:
$stmt = $conn->prepare("SELECT * FROM hotel
INNER JOIN booking
ON hotel.hotel_id=booking.hotel_id
INNER JOIN guest
ON guest.guest_id=booking.guest_id
WHERE name LIKE :search_term");
然而,这个没有用,它给了我一个空的结果
$stmt = $conn->prepare("SELECT * FROM guest
INNER JOIN booking
ON guest.guest_id=booking.guest_id
INNER JOIN hotel
On booking.hotel_id=hotel.hotel_id
WHERE guest_id LIKE :search_term");
$stmt->bindValue(':search_term','%'.$search_term. '%');
我的完整代码是:
<!DOCTYPE html>
<html>
<head>
<title>Database</title>
<link href="style.css" rel="stylesheet" type="text/css"> <!-- This is linking style sheet (css)into this HTML page-->
<link href='https://fonts.googleapis.com/css?family=PT+Serif:400italic' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="navigation">
<form action = "index.php" method="get">
<input type = "submit" name = "mainpage" value = "Main Page" class = "submitbut" id = "but1" />
</form>
</div>
<form action="index.php" method="post">
<input type = "text" name = "search" id = "searching" />
<input type = "submit" name = "data_submit" value = "Search" id = "scan" />
</form>
<?php
if(isset($_GET['mainpage'])){
header("Location:mainpage.php");
exit;
}
if (isset($_POST["data_submit"])){
$search_term = $_POST['search'];
$conn = new PDO(
'mysql:host=localhost;dbname=u1358595',
'root'
);
$stmt = $conn->prepare("SELECT * FROM hotel
INNER JOIN booking
ON hotel.hotel_id=booking.hotel_id
INNER JOIN guest
ON guest.guest_id=booking.guest_id
WHERE name LIKE :search_term");
$stmt->bindValue(':search_term','%'.$search_term. '%');
$stmt->execute();
echo
"<table><tr>
<th>Results</th>
</tr>";
while($hotel = $stmt->fetch())
{
echo
"<tr>"."<td>"."<a href='details.php?name=".$hotel['name']."'>".$hotel['name']."</a>"."</td>"."</tr>";
}
echo "</table>";
$stmt = $conn->prepare("SELECT * FROM guest
INNER JOIN booking
ON guest.guest_id=booking.guest_id
INNER JOIN hotel
On booking.hotel_id=hotel.hotel_id
WHERE guest_id LIKE :search_term");
$stmt->bindValue(':search_term','%'.$search_term. '%');
$stmt->execute();
echo
"<table><tr>
<th>Results</th>
</tr>";
while($hotel = $stmt->fetch())
{
echo
"<tr>"."<td>"."<a href='details.php?name=".$hotel['first_name']."'>".$hotel['last_name']."</a>"."</td>"."</tr>";
}
echo "</table>";
$conn = NULL;
}
?>
</body>
</html>
答案 0 :(得分:2)
你的WHERE子句有问题。您没有定义比较哪个表列guest_id。 guest_id
应该替换为我相信的guest.guest_id
。
$stmt = $conn->prepare("SELECT * FROM guest
INNER JOIN booking
ON guest.guest_id=booking.guest_id
INNER JOIN hotel
On booking.hotel_id=hotel.hotel_id
WHERE guest.guest_id LIKE :search_term");
$stmt->bindValue(':search_term','%'.$search_term. '%');
编辑:
此外,搜索部分可以替换为:
... '...WHERE guest.guest_id LIKE "%":search_term"%"');
$stmt->bindValue(':search_term', $search_term);
您需要为查询使用单引号。
答案 1 :(得分:0)
考虑使用MATCH AGAINST:documentation
SELECT * FROM hotel
INNER JOIN booking
ON hotel.hotel_id=booking.hotel_id
INNER JOIN guest
ON guest.guest_id=booking.guest_id
WHERE MATCH (col1, col2)
AGAINST ('+keyword1' IN BOOLEAN MODE);