gpt4 book ai didi

php - 使用 PhP 通过 SQL 进行搜索

转载 作者:行者123 更新时间:2023-11-29 07:28:01 24 4
gpt4 key购买 nike

我正在尝试创建一个搜索工具,在两个不同的表中搜索 2 个不同的列,因此如果酒店表中的名称匹配或宾客表中的 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>

最佳答案

您的 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);

不过,您需要在查询中使用单引号。

关于php - 使用 PhP 通过 SQL 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33824422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com