2014年8月23日星期六

php文章搜索实例代码

<?php
//站内搜索
/*
* SQL模糊查询
* select * form fengxd where fengname like '冯%';
* 搜索以冯开头的
* select * form fengxd where fengname like '%冯';
* 搜索以冯结束的
* select * form fengxd where fengname like '%冯%';
* 搜索包含冯的
* %号相当于*号,代表任意多个字符;
* _号相当于?,代表一个字符;这个用得少;
*/
$con=mysqli_connect("localhost","root","") or die (mysqli_error());
mysqli_select_db($con,'test');
if(isset($_POST['submit'])){
if(!empty($_POST['title'])&&!empty($_POST['author'])){
$title=mysqli_real_escape_string($con,trim($_POST['title']));
$author=mysqli_real_escape_string($con,trim($_POST['author']));
$ins="insert into wenzhang (title,author,date) values('$title','$author',now())";
$query=mysqli_query($con,$ins) or die(mysqli_error());
}else
{
echo "input all information";
}
}

if(isset($_GET['key'])){
$key=mysqli_real_escape_string($con,trim($_GET['key']));
$sql="select * from wenzhang where title like '%$key%'";
$query2=mysqli_query($con,$sql);
while ($row=mysqli_fetch_array($query2)){
echo $row['title']."<br>";
}
}
?>
<html>
<form action="" method="post">
title<input type="text" name="title"><br>
author<input type="text" name="author"><br>
<input type="submit" name="submit"><br>
</form>
<div>
<form action="" method="get">
Search<input type="text" name="key"><br>
<input type="submit" name="search"><br>
</form>
</div>
</html>

 

没有评论:

发表评论