php+mysql新闻发布系统(六)

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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值