好的,基本上我遇到了从搜索工具输出结果的问题。
所以,让我们说例如我搜索并选择一家酒店' hotel_id' = 4而不是总是输出' hotel_id'无论我选择什么,都= 1。搜索工具搜索酒店名称和客人的姓氏,并且选择它应输出与之匹配的任何内容,这应该是客人的详细信息,他们所做的预订以及他们选择的酒店。 / p>
我的搜索工具如下所示:
<!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
答案 0 :(得分:0)
我更改了被识别为可能不正确的部分并进行了一些整理 - 看起来它应该有效,当然基于搜索选择记录应该没有错误
<!--
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>