【问题标题】:PHP SQL Database How to program a Search with FilterPHP SQL 数据库如何使用过滤器编写搜索程序
【发布时间】:2018-01-18 16:50:17
【问题描述】:

下面是我的 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 时,用户可以搜索存储在数据库中的特定作者或标题。基本上是过滤搜索。任何人都可以建议我应该如何去做吗?谢谢 !

【问题讨论】:

  • 仅发布与问题相关的代码/标记。例如,您的 CSS 不相关。
  • 只需为要过滤的字段添加更多WHERE-clauses。此外,您应该使用Prepared Statements,而不是像这样连接查询中的变量。
  • @MagnusEriksson 我应该如何添加 WHERE 子句?语法是什么样的?是在 $sql 语句中吗?
  • 您当前的查询中已经有一个WHERE-statement。只需添加AND colname = :paramname AND colname2 = :paramname2 等即可添加更多条件。为了了解 Prepared Statements 的工作原理,我在第二条评论中链接到手册。
  • @MagnusEriksson 感谢关于在 WHERE 子句中使用 AND 的建议。我使用 AND 和 LIKE % 语句让它工作。现在它的工作谢谢。请帮我将此问题标记为已回答谢谢。

标签: php mysql database search filter


【解决方案1】:

您需要做的就是在您当前的WHERE-statement 中添加更多条件。

要添加更多条件,请使用AND-运算符,如下所示:

SELECT col1, col2, col3, col4  FROM table WHERE col1 = 'x' AND col2 = 'y' AND col3 = 'z'

你可以有多个AND-operators,你可以检查任何列。

阅读更多关于logic operators in MySQL's documentation的信息。

为了确保您的查询安全,您应该更新它们以使用准备好的语句,而不是像您当前正在做的那样连接值。你可以阅读更多关于Prepared Statements in PHP's manual的信息。

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 2021-12-12
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多