下面是我的searchData.php,它搜索数据库。
<html>
<head>
<title>Search Results</title>
<style type="text/css">
body {
font-size: 15px;
color: #343d44;
font-family: "segoe-ui", "open-sans", tahoma, arial;
padding: 0;
margin: 0;
background-image: url("invoker.jpg");
}
table {
margin: auto;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
font-size: 12px;
width: 100%;
color: gold;
font-weight: bold;
}
h1 {
margin: 25px auto 0;
text-align: center;
text-transform: uppercase;
font-size: 17px;
}
table td {
transition: all .5s;
}
.data-table {
border-collapse: collapse;
font-size: 14px;
min-width: 537px;
}
.data-table th,
.data-table td {
border: 1px solid #e1edff;
padding: 7px 17px;
}
.data-table caption {
margin: 7px;
}
.data-table thead th {
background-color: #508abb;
color: #FFFFFF;
border-color: #6ea1cc !important;
text-transform: uppercase;
}
.data-table tbody td {
color: #353535;
}
.data-table tbody td:first-child,
.data-table tbody td:nth-child(4),
.data-table tbody td:last-child {
text-align: right;
}
.data-table tbody tr:nth-child(odd) td {
background-color: #f4fbff;
}
.data-table tbody tr:hover td {
background-color: #ffffa2;
border-color: #ffff0f;
}
.data-table tfoot th {
background-color: #e5f5ff;
text-align: right;
}
.data-table tfoot th:first-child {
text-align: left;
}
.data-table tbody td:empty
{
background-color: #ffcccc;
}
</style>
</head>
<body>
<h1><u>Search Reviews</u></h1>
<table class="data-table">
<thead>
<tr>
<th>FaceBook User</th>
<th>Author</th>
<th>Title</th>
<th>Date</th>
<th>Age</th>
<th>Review</th>
</tr>
</thead>
<tbody>
<?php
session_start();
$friends = $_SESSION["friends"];
define ("DB_USER", "yeospace_nm0102");
define ("DB_PASSWORD", "ap3545");
define ("DB_HOST", "localhost");
define ("DB_NAME", "yeospace_nm0102");
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
$author=$_POST["author"];
$title=$_POST["title"];
$date=$_POST["date"];
$age=$_POST["age"];
$review=$_POST["review"];
echo "<table>";
foreach ($friends as $friend){
$sql="SELECT author, title, date, age, review FROM AddEntry WHERE fbUserID='$friend[id]'";
$result = mysqli_query($dbc, $sql);
if ($result){
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td>".$friend[name]."</td><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td><td>".$row[4]."</td>";
echo "</tr>";
};
}
}
echo "</table>";;
mysqli_close($dbc) ;
?>
</tbody>
</table>
<button onClick="location.href='index.php'">Return to Home</button>
</body>
</html>
这是searchEntry.php,基本上就是搜索表单。
<html>
<head>
<title>Search Reviews</title>
<link type="text/css" rel="stylesheet" href="style2.css"/>
</head>
<body>
<form action="searchData.php" method="post">
<h1>Search Review</h1>
<fieldset>
<legend><span class="number">1</span>Search Review Info</legend>
<label for="author">Author:</label>
<input type="author" id="author" name="author">
<label for="title">Title:</label>
<input type="title" id="title" name="title">
<label for="date">Date:</label>
<input type="date" id="date" name="date">
<label>Age:</label>
<input type="radio" id="under_13" value="under_13" name="age"><label for="under_13" class="light">Under 13</label><br>
<input type="radio" id="over_13" value="over_13" name="age"><label for="over_13" class="light">13 or older</label>
</fieldset>
<fieldset>
<legend><span class="number">2</span>Search Reviews</legend>
<label for="review">Review:</label>
<textarea id="review" name="review"></textarea>
</fieldset>
<button type="search"> Search Reviews </button>
</form>
</body>
</html>
目前,我的搜索功能几乎与我的ListEntry.php一样好,后者列出了数据库中的所有内容。我需要这样做,当人们使用我的searchEntry.php时,用户可以搜索存储在数据库中的特定作者或标题。基本上是过滤搜索。任何人都可以建议我应该怎么做呢?谢谢 !
答案 0 :(得分:0)
您需要做的就是为当前的WHERE
语句添加更多条件。
要添加更多条件,请使用AND
- 运算符,如下所示:
SELECT col1, col2, col3, col4 FROM table WHERE col1 = 'x' AND col2 = 'y' AND col3 = 'z'
您可以拥有多个AND
- 运算符,您可以检查任何列。
详细了解logic operators in MySQL's documentation。
为了确保您的查询安全,您应该更新它们以使用预备语句而不是像您当前所做的那样连接值。您可以阅读有关Prepared Statements in PHP's manual的更多信息。