不管发生什么,Php都会输出相同的结果
好的,基本上我在从搜索设备输出结果时遇到了问题 比如说,我搜索并选择了一个“hotel_id”=4的酒店,不管我选择什么,它总是输出“hotel_id”=1的结果。搜索功能通过酒店名称和客人姓氏进行搜索,无论选择哪一个,都会输出与之匹配的任何内容,包括客人的详细信息、预订情况以及他们选择的酒店 我的搜索工具如下所示:不管发生什么,Php都会输出相同的结果,php,sql,Php,Sql,好的,基本上我在从搜索设备输出结果时遇到了问题 比如说,我搜索并选择了一个“hotel_id”=4的酒店,不管我选择什么,它总是输出“hotel_id”=1的结果。搜索功能通过酒店名称和客人姓氏进行搜索,无论选择哪一个,都会输出与之匹配的任何内容,包括客人的详细信息、预订情况以及他们选择的酒店 我的搜索工具如下所示: <!DOCTYPE html> <html> <head> <title>Database</title> &l
<!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>Hotels Matched</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.last_name LIKE :search_term");
$stmt->bindValue(':search_term','%'.$search_term. '%');
$stmt->execute();
echo
"<table><tr>
<th>Guests Matched</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>
数据库
我的结果打印在另一页上,代码是:
<!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'>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</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>
<?php
$conn = new PDO(
'mysql:host=localhost;dbname=u1358595',
'root'
);
if(!isset($_GET['name']))
{
echo "You shouldn't have got to this page";
exit;
}
$name = $_GET['name'];
$query = "SELECT * FROM hotel WHERE name=$name";
$stmt = $conn->prepare($query);
$stmt->bindValue(':name',$name);
$stmt->execute();
echo
"<table><tr>
<th>hotel_id</th>
<th>name</th>
<th>address</th>
<th>postcode</th>
<th>town</th>
<th>description</th>
<th>rating</th>
<th>image</th></tr>";
while($hotel=$stmt->fetch());
{
echo
"<td>". $hotel['hotel_id']."</td>".
"<td>". $hotel['name']."</td>".
"<td>". $hotel['address']."</td>".
"<td>". $hotel['postcode']."</td>".
"<td>". $hotel['town']."</td>".
"<td>". $hotel['description']."</td>".
"<td>". $hotel['rating']."</td>".
"<td>"."<img src='". $hotel['image']. "'>"."</td>"."</tr>";
//$variable = $hotel['hotel_id'];
}
echo "</table>";
/*
$query2 = "SELECT * FROM booking WHERE hotel_id=$variable";
echo
"<table><tr>
<th>hotel_id</th>
<th>guest_id</th>
<th>payment-type</th>
<th>amount</th>
<th>nights</th></tr>";
$results2 = $conn->query($query2);
if($variable = $results2->fetch()) { echo
"<tr>"."<td>".$variable['hotel_id']."</td>".
"<td>". $variable['guest_id']."</td>".
"<td>". $variable['payment-type']."</td>".
"<td>". "£".$variable['amount']."</td>".
"<td>". $variable['nights']."</td>"."</tr>";
$guest_id = $variable['guest_id'];
}
echo "</table>";
$query3 = "SELECT * FROM guest WHERE guest_id=$guest_id";
echo
"<table><tr>
<th>guest_id</th>
<th>first_name</th>
<th>last_name</th>
<th>address</th>
<th>postcode</th>
<th>town</th></tr>";
$results3 =$conn->query($query3);
while($guest = $results3->fetch()) { echo
"<tr>"."<td>".$guest['guest_id']."</td>".
"<td>". $guest['first_name']."</td>".
"<td>". $guest['last_name']."</td>".
"<td>". $guest['address']."</td>".
"<td>". $guest['postcode']."</td>".
"<td>". $guest['town']."</td>"."</tr>";
}
echo "</table>";
*/
$conn=NULL;
?>
</body>
</html>
数据库
我被困在这几天了,所以请帮助我使这项工作,如果有任何方法,以减少和不重复相同的代码一遍
谢谢你的努力;)
请注意,我不允许使用javascript我更改了被识别为可能不正确的部分,并进行了一些整理-看起来应该可以工作,当然基于搜索的记录选择应该没有问题
<!--
Search ( assumed to be index.php )
----------------------------------
As this page displays results from the search I assume that this
is "index.php" and that the form `POST`s back to this page.
-->
<?php
error_reporting( E_ALL );
?>
<!DOCTYPE html>
<html>
<head>
<title>Hotel Database Search</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
/* This is liable to cause an error: headers should not be sent after any output */
if( isset( $_GET['mainpage'] ) ) exit( header( "Location: mainpage.php" ) );
if ( isset( $_POST["data_submit"] ) ){
$search_term = strip_tags( trim( $_POST['search'] ) );
$conn = new PDO( 'mysql:host=localhost;dbname=u1358595', 'root' );
$stmt = $conn->prepare("SELECT * FROM `hotel` h
INNER JOIN `booking` b ON h.`hotel_id`=b.`hotel_id`
INNER JOIN `guest` g ON g.`guest_id`=b.`guest_id`
WHERE `name` LIKE :search_term;");
$stmt->bindValue(':search_term','%' . $search_term . '%');
$stmt->execute();
echo "<table><tr><th>Hotels Matched</th></tr>";
while( $rs= $stmt->fetch(PDO::FETCH_OBJ) ) {
echo "<tr><td><a href='details.php?name=".$rs->name."'>".$rs->name."</a></td></tr>";
}
echo "</table>";
$stmt = $conn->prepare("SELECT * FROM `guest` g
INNER JOIN `booking` b ON g.`guest_id`=b.`guest_id`
INNER JOIN `hotel` On b.`hotel_id`=h.`hotel_id`
WHERE g.`last_name` LIKE :search_term;");
$stmt->bindValue( ':search_term', '%'.$search_term.'%' );
$stmt->execute();
echo "<table><tr><th>Guests Matched</th></tr>";
while( $rs= $stmt->fetch(PDO::FETCH_OBJ) ) {
echo "<tr><td><a href='details.php?name=".$rs->first_name."'>".$rs->last_name."</a></td</tr>";
}
echo "</table>";
$conn = NULL;
}
?>
</body>
</html>
<!--
Results ( assumed to be "details.php" )
---------------------------------------
I again assume that this is the details page and this is linked to
via the results found on "index.php"
-->
<!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'>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</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>
<?php
if( !isset( $_GET['name'] ) ) exit("You shouldn't have got to this page");
$conn = new PDO( 'mysql:host=localhost;dbname=u1358595', 'root' );
$name = $_GET['name'];
$query="SELECT * FROM `hotel` WHERE `name`=:name;";
$stmt = $conn->prepare( $query );
$stmt->bindValue( ':name', $name );
$stmt->execute();
echo
"<table>
<tr>
<th>hotel_id</th>
<th>name</th>
<th>address</th>
<th>postcode</th>
<th>town</th>
<th>description</th>
<th>rating</th>
<th>image</th>
</tr>";
while( $rs=$stmt->fetch(PDO::FETCH_OBJ) ){
echo "
<tr>
<td>{$rs->hotel_id}</td>
<td>{$rs->name}</td>
<td>{$rs->address}</td>
<td>{$rs->postcode}</td>
<td>{$rs->town}</td>
<td>{$rs->description}</td>
<td>{$rs->rating}</td>
<td><img src='{$rs->image}'></td>
</tr>";
}
echo "</table>";
$conn=NULL;
?>
</body>
</html>
酒店数据库搜索
数据库
当然这里有一个错误$query=“SELECT*FROM hotel WHERE name=$name”$stmt=$conn->prepare($query)$stmt->bindValue(':name',$name)代码>~ sql中应该有一个名为:name
的占位符,而不是实际的变量和另一个错误($hotel=$stmt->fetch());{
~那里有一个不应该存在的分号。循环中没有打开的
标记。数据库中的列称为“name”因此,查询基本上是说,然后其中column\u name=column\u name
?从上一页检索或应该检索$name并存储在该变量中,然后应该在该值和数据库中的值之间进行比较,如果两者都匹配,则输出结果,这似乎起到了作用,但现在它只起作用搜索酒店名称,但出于某些原因,忽略了按客人姓氏搜索的部分。当然,虽然您用于搜索酒店的搜索条件不太可能返回客人的姓氏
,是吗?问题是,我需要提供多个搜索选项,因此在这一项中,我提供了一个按姓氏搜索的选项酒店或按客人-要查找他们的预订,它必须按姓氏搜索,然后输出他们的详细信息和他们预订的酒店详细信息编辑://搜索页面中甚至有代码显示按姓氏搜索可能你误解了我的意思或我的意思-但假设你搜索一家名为“Hilton”的酒店
然后你会很容易地匹配酒店,也许也会匹配作为客人的“帕里斯希尔顿”
,但是如果你搜索“宜必思”
,不太可能有一位客人的姓氏为“宜必思”/姓氏
哦,我明白你的意思,但如果我输入“W”,它应该会输出我的酒店和姓氏“W”但不是,是的,但只针对酒店,而不是姓氏,5个姓氏包含W,所以我应该得到一个输出