search.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/7/12
* Time: 21:12
*/
header('Content-Type:text/html;charset=utf-8');
?>
<html>
<head>
<title>搜索新闻</title>
<script type="text/javascript">
function dodel(id) {
if(confirm("确定要删除吗?")) {
window.location = "action.php?action=del&id=" + id;
}
}
</script>
</head>
<body>
<center>
<form action="search.php" method="post">
<input type="text" name="search" placeholder="请输入查询内容">
<input type="submit" value="搜索">
</form>
<table width="800" border="1" left align="center" cellpadding='0' cellspacing = '0' >
<tr>
<th width='8%'>新闻id</th>
<th width='15%'>新闻标题</th>
<th width='12%'>关键字</th>
<th width='12%'>作者</th>
<th width='20%'>发布时间</th>
<th width='12%'>新闻内容</th>
<th width='12%'>操作</th>
</tr><?php
include ("menu.php");
include ("dbconfig.php");
header('Content-Type:text/html;charset=utf-8');
$link = mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
mysqli_select_db($link,DB_NAME);
// $sql = "select * from news where title={$_POST["title"]} or keywords={$_POST["keywords"]} or author={$_POST["author"]}";
// $search = $_POST['search'];
// $search = isset($_POST['title'])?trim($_POST['$title']):""; //isset判断是否存在函数,trim去除字符串首尾空白字符
if (empty($search=$_POST['search'])){
$sql = "select * from news";
}else {
$sql = "select * from news where title like '$search%' or author like '$search%'or keywords like '$search%'";
}
$result = mysqli_query($link,$sql);
while ($new = mysqli_fetch_assoc($result)){
echo "<table width='800' border='1' cellspacing='0' cellpadding='0'>";
echo "<tr align='center'>";
echo "<td width='8%'>{$new['id']}</td>";
echo "<td width='15%'>{$new['title']}</td>";
echo "<td width='12%'>{$new['keywords']}</td>";
echo "<td width='12%'>{$new['author']}</td>";
echo "<td width='20%'>".date('Y-m-d H:m:s',$new['addtime'])."</td>";
echo "<td width='12%'>{$new['content']}</td>";
echo "<td width='12%'>
<a href = 'javascript:dodel({$new['id']})'>删除</a>
<a href='edit.php?id={$new['id']}'>修改</a> </td>";
echo "</tr>";
echo "</table>";
}
?>
</table>
</center>
</body>
</html>
1、存在一个问题,如上图,第一次进入搜索页面会有错误提示。
2、可以根据作者、关键字、标题搜索,但是可以更加优化,采用选项式进行搜索。
作者纯属小白,各位大佬有问题可以找我,大家一起探讨哦,附加联系方式
Q Q:1799158338
微信:15903486297