• PHP留言小练习


    实现功能:

      留言、搜索、编辑、删除、详情页、时间、点击量

    页面划分:  

      index.html(留言列表页)
      add.html(留言页)
      edit.php(编辑页)
      del.php(删除页)
      view.php(详情页)
      conn.php(数据库连接)

    数据字段:

      

    视图呈现:

      

    代码展现:

    index.php

    <link rel="stylesheet" href="css/main.css">
    <div class="g-wrap">
        <div class="m-top">
            <a class="mt-link" href="add.php">留言</a>
            <div class="mt-search">
                <form action="" name="keys">
                    <input type="text" name="keys">
                    <input type="submit" value="搜索">
                </form>
            </div>
        </div>
        <div class="m-con">
            <ul class="m-infor">
                <?php
                    include("conn.php");//引入数据库
                    if(!empty($_GET['keys'])){
                        $w = " `title` like '%".$_GET['keys']."%' ";
                    }
                    else{
                        $w = 1;
                    }
                    $sql = "select * from `news` where $w order by id desc limit 10";//倒序显示10条,where 1表示没有任何条件和不写一样
                    $query = mysql_query($sql);//执行sql语句并返回资源
                    //$rs = mysql_fetch_array($query);//返回双重数组,下标和键名都有,每执行一次得到一条
                    while( ($rs = mysql_fetch_array($query)) != false ){
                ?>
                <li class="mi-list">
                    <h3 class="ml-top">
                        <a href="view.php?id=<?php echo $rs['id']; ?>">
                            <?php echo $rs['title']; ?>
                        </a>
                    </h3>
                    <div class="ml-cen">
                        <?php echo iconv_substr($rs['contents'],0,10,"gbk"); ?>...
                    </div>
                    <div class="ml-bot">
                        <span class="mb-l"><?php echo $rs['dates']; ?></span>
                        <div class="mb-r">
                            <a href="edit.php?id=<?php echo $rs['id']; ?>">编辑</a>
                            <a href="del.php?del=<?php echo $rs['id']; ?>">删除</a>
                        </div>
                    </div>
                </li>
                <?php
                    }
                ?>
            </ul>
        </div>
    </div>

    add.php

    <link rel="stylesheet" href="css/main.css">
    <?php 
        include("conn.php");//引入链接数据库
        if( !empty($_POST["sub"]) ){//第一次返回真empty($_POST["sub"])这里也可以用其他值判断
            $title = $_POST['title'];
            $con = $_POST['con'];
        
            $sql = "insert into `news` (`id`,`title`,`dates`,`contents`,`hits`) values (null,'$title',now(),'$con',0)";
        
            mysql_query($sql);//执行sql语句
        
            $url = "./index.php";
            
            echo "<script>location.href='".$url."';</script>";
            //print_r($sql);
        }
    ?>
    <div class="g-wrap">
        <div class="m-form">
            <form action="add.php" method="post">
                <ul>
                    <li>
                        <label for="">标题:</label>
                        <input type="text" name="title">
                    </li>
                    <li>
                        <label for="">内容:</label>
                        <textarea id="" cols="30" rows="10" name="con"></textarea>
                    </li>
                    <li>
                        <input type="submit" name="sub" value="发表">
                    </li>
                </ul>
            </form>
        </div>
    </div>

    edit.php

    <link rel="stylesheet" href="css/main.css">
    <?php
        include("conn.php");
        if( !empty($_GET['id']) ){
            $sql = "select * from news where `id`='".$_GET['id']."'";
            $query = mysql_query($sql);//执行sql语句
            
            $rs = mysql_fetch_array($query);
            //print_r($rs);
        }
        //执行update语句
        if(!empty($_POST["sub"])){//第一次返回真empty($_POST["sub"])这里也可以用其他值判断 这里只有点提交的时候才执行嘛
        
            $title = $_POST['title'];
            $con = $_POST['con'];
            $hid = $_POST['hid'];//这里也可以通过get获取值与之对应起来
            $sql = "update `news` set `title`='$title',`contents`='$con' where id='$hid' limit 1 ";
        
            mysql_query($sql);//执行sql语句
        
            //echo "更新成功";
            echo "<script>alert('更新成功!');location.href='index.php';</script>";
        }
    ?>
    <div class="g-wrap">
        <div class="m-form">
            <form action="edit.php" method="post">
                <input type="hidden" name="hid" value="<?php echo $rs['id']; ?>">
                <ul>
                    <li>
                        <label for="">标题:</label>
                        <input type="text" name="title" value="<?php echo $rs['title']; ?>">
                    </li>
                    <li>
                        <label for="">内容:</label>
                        <textarea id="" cols="30" rows="10" name="con"><?php echo $rs['contents']; ?></textarea>
                    </li>
                    <li>
                        <input type="submit" name="sub" value="发表">
                    </li>
                </ul>
            </form>
        </div>
    </div>

    del.php

    <?php
        include("conn.php");
        
        if(!empty($_GET['del'])){//empty()当为空时返回真
            $d = $_GET['del'];
            $sql = "delete from `news` where `id`='$d'";
            
            mysql_query($sql);//执行sql语句
            $url = "./index.php";
            echo "<script>alert('删除成功!');location.href='".$url."';</script>";
        }
    ?>

    view.php

    <link rel="stylesheet" href="css/main.css">
    <?php
        include("conn.php");//引入链接数据库
        if(!empty($_GET['id'])){
            $sql = "select * from news where `id`='".$_GET['id']."'";
            $query = mysql_query($sql);//执行sql语句
            
            $rs = mysql_fetch_array($query);
            
            //print_r($rs);
            
            $sqlup = "update news set hits=hits+1 where `id`='".$_GET['id']."'";
            mysql_query($sqlup);
            
        }
    ?>
    <div class="g-wrap m-infor-details">
        <h1 class="mid-title"><?php echo $rs['title']; ?></h1>
        <p class="mid-con">
            <?php echo $rs['contents']; ?>
        </p>
        <div class="mid-bot">
            <span class="mid-b-l">时间:
                <?php echo $rs['dates']; ?>   
            </span>
            <span class="mid-b-r">点击量:
                <?php echo $rs['hits']; ?>
            </span>
        </div>
    </div>

    conn.php

    <?php
        //@不把错误暴露出来
        @mysql_connect("localhost:3306","root","123456") or die("mysql链接失败");
        @mysql_select_db("test") or die("db链接失败");
        
        //mysql_set_charset("gbk");//php5.2.3以上
        mysql_query("set names 'gbk'");
    ?>

    main.css

    *{
        margin: 0;
        padding: 0;
    }
    li{
        list-style-type: none;
    }
    body{
        background-color: #1B9102;
        font-size: 14px;
    }
    /*....................................index.html...........*/
    .g-wrap{
        margin: 50px auto 0;
        width: 500px;
        min-height: 300px;
        box-shadow: 0 0 10px #fff;
    }
    .m-top{
        background-color: #d2d2d2;
        overflow: hidden;
    }
    .mt-link{
        float: left;
    }
    .mt-search{
        float: right;
        font-size: 0;
    }
    [type=submit]{
        cursor: pointer;
    }
    .mi-list{
        color: #adadad;
        padding-top: 20px;
        border-bottom: 1px dashed #fff;
    }
    .ml-top{
        color: #333;
    }
    .ml-cen{
        margin-top: 10px;
        font-size: 12px;
    }
    .ml-bot{
        height: 50px;
        line-height: 50px;
        margin-top: 10px;
    }
    .mb-l{
        float: left;
        color: pink;
    }
    .mb-r{
        float: right;
        cursor: pointer;
    }
    /*......................................add.html...........*/
    .m-form{
        padding: 20px;
        box-sizing: border-box;
    }
    .m-form li{
        margin-bottom: 20px;
    }
    .m-form label{
        color: #fff;
        vertical-align: top;
    }
    /*......................................view.html...........*/
    .m-infor-details{
        padding: 20px 20px 50px;
        box-sizing: border-box;
        position: relative
    }
    .mid-title{
        color: #fff;
        text-align: center;
    }
    .mid-con{
        color: #a9a9a9;
        font-size: 14px;
        margin-top: 20px;
        text-indent: 30px;
    }
    .mid-bot{
        position: absolute;
        right: 20px;
        bottom: 0;
        left: 20px;
        height: 50px;
        line-height: 50px;
        color: #fff;
    }
    .mid-b-l{
        float: left;
    }
    .mid-b-r{
        float: right;
    }

    代码下载

      

  • 相关阅读:
    用Python完成一个汇率转换器
    鸿蒙如何用JS开发智能手表App
    鸿蒙如何用JS开发智能手表App
    SAP Spartacus SplitViewComponent Migration 的一个具体例子
    SAP Spartacus B2B 页面 Popover Component 的条件显示逻辑
    SAP Spartacus 升级时关于 schematics 的更新
    SAP Spartacus B2B 页面 Disable 按钮的显示原理
    SAP Spartacus B2B 页面 Disable Confirmation 对话框的显示原理
    通过 Feature Level 动态控制 SAP Spartacus 的页面显示
    SAP Commerce Cloud Build Manifest Components
  • 原文地址:https://www.cnblogs.com/jiujiaoyangkang/p/5585729.html
Copyright © 2020-2023  润新知