PHP实现搜索功能

PHP实现搜索功能

写在前面

‘’ php5与php7使用MySQL时有mysql和mysqli的区别,参数位置的变化;在不在同一页面主要取决于结果页面是否有搜索页面的代码

PHP7

搜索页面与结果在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysqli_select_db($conn,"database");

		mysqli_query($conn,"set names utf8");
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysqli_query($conn,$sql);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h1>搜索</h1><br/>
	<form action='search.php' method='post'>
		收录内容:<input type='text' name='keywords' placeholder='搜索不到?可能还没被收录哦~'>
		<input type='submit' values='搜索'>
	</form>";
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysqli_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysqli_close($conn);

?>

搜索页面与结果不在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysqli_select_db($conn,"database");

		mysqli_query($conn,"set names utf8");
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysqli_query($conn,$sql);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysqli_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysqli_close($conn);

?>

PHP5

搜索页面与结果在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysql_select_db("database",$conn);

		mysql_query("set names utf8",$conn);
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysql_query($sql,$conn);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h1>搜索</h1><br/>
	<form action='search.php' method='post'>
		收录内容:<input type='text' name='keywords' placeholder='搜索不到?可能还没被收录哦~'>
		<input type='submit' values='搜索'>
	</form>";
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysql_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysql_close($conn);

?>

搜索页面与结果不在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysql_select_db("database",$conn);

		mysql_query("set names utf8",$conn);
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysql_query($sql,$conn);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysql_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysql_close($conn);

?>
上一篇:brute force


下一篇:堆叠注入