• php学习实例3


      新闻发布管理系统

      路由器action.php

    <!DOCTYPE html>
    <html>
    <head>
        <title>
            
        </title>
        <meta charset="utf-8">
    </head>
    <body>
    <?php
        require("dbconfig.php");
        $link = @mysql_connect(HOST, USER, PASS) or die("db error");
        mysql_select_db(DBNAME, $link);
    
        echo "action";
        switch($_GET["action"]) {
            case "add" :
                 $title = $_POST["title"];
                 $keywords = $_POST["keywords"];
                 $author = $_POST["author"];
                 $content = $_POST["content"];
                 $addtime = time();
    
                 $sql = "insert into news values(null, '{$title}', '{$keywords}', '{$author}', {$addtime}, '{$content}')";
                 //echo $sql;
                 $link.mysql_query($sql);
                 $id = mysql_insert_id($link);
                 if($id>0) {
                     echo "<a>成功</a>";
                 }else{
                     echo "<a>失败</a>";
                 }
                 echo "<a href='./index.php'>返回主页</a>";
            break;
            case "del" :
                $id = $_GET["id"];
                $sql = "delete from news where id={$id}";
                mysql_query($sql);
                header("Location:index.php");
            break;
            case "update" :
                 $id = $_POST["id"];
                 $title = $_POST["title"];
                 $keywords = $_POST["keywords"];
                 $author = $_POST["author"];
                 $content = $_POST["content"];
                 $addtime = time();
    
                 $sql = "update  news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}',addtime={$addtime} where  id = ${id}";
    
    
                 //echo $sql;
    
                 $result = mysql_query($sql);
                 header("Location:index.php");
            break;
        }
    
        mysql_close($link);
    ?>
    </body>
    </html>

      统一导航栏menu.php

    <ul>
        <li>
            <a href="index.php">浏览新闻</a>
        </li>
        <li>
            <a href="add.php">发布新闻</a>
        </li>
    </ul>

      首页index.php

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
    </head>
    <body>
        <?php 
            include("menu.php");
        ?>
        <table border="1">
            <thead>
                <tr>
                    <th>id</th>
                    <th>标题</th>
                    <th>关键字</th>
                    <th>作者</th>
                    <th>时间</th>
                    <th>内容</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    require("dbconfig.php");
                    $link = @mysql_connect(HOST, USER, PASS) or die ("mysql error");
                    mysql_select_db("newsdb", $link);
    
                    $sql = "select * from news order by addtime desc";
                    $result = mysql_query($sql, $link);
                    while($row = mysql_fetch_assoc($result)) {
                        echo "<tr>";
                        echo "<td>{$row['id']}</td>";
                        echo "<td>{$row['title']}</td>";
                        echo "<td>{$row['keywords']}</td>";
                        echo "<td>{$row['author']}</td>";
                        echo "<td>".date("Y-m-d",$row['addtime'])."</td>";
                        echo "<td>{$row['content']}</td>";
                        echo "<td><a href='./action.php?action=del&id={$row['id']}'>删除</a> 
    
                            <a href='./edit.php?id={$row['id']}'>修改</a>
                            </td>";
                        echo "</tr>";
                    };
                    mysql_free_result($result);
                    mysql_close($link);
                ?>
                <tr>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </body>
    </html>

      数据库管理 dbconfig.php

    <?php
        define("HOST","localhost");
        define("USER","root");
        define("PASS","");
        define("DBNAME","newsdb");
    ?>

      数据添加add.php

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
    </head>
    <body>
        <?php
            include("menu.php");    
        ?>
        <form action="action.php?action=add" method="post">
            <table border="1">
                <tbody>
                    <tr>
                        <td>标题</td>
                        <td>
                            <input type="text" name="title">
                        </td>
                    </tr>
                    <tr>
                        <td>关键字</td>
                        <td>
                            <input type="text" name="keywords">
                        </td>
                    </tr>
                    <tr>
                        <td>作者</td>
                        <td>
                            <input type="text" name="author">
                        </td>
                    </tr>
                    <tr>
                        <td>内容</td>
                        <td>
                            <textarea name="content" rows="5" ></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="add">
                            <input type="reset" value="reset">
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
    </html>

      数据编辑edit.php

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
    </head>
    <body>
        <?php
            include("menu.php");
            require("dbconfig.php");
            $link = @mysql_connect(HOST, USER, PASS) or die ("mysql error");
            mysql_select_db("newsdb", $link);
            $id = $_GET['id'];
            $sql = "select * from news where id='{$id}'";
            $result = mysql_query($sql, $link);
            if($result && mysql_num_rows($result)>0) {
                $news = mysql_fetch_assoc($result);
            }else{
                echo "no data";
            };
        ?>
        <form action="action.php?action=update" method="post">
            <table border="1">
                <input type="hidden" name="id" value="<?php echo $news['id'] ?>">
                <tbody>
                    <tr>
                        <td>标题</td>
                        <td>
                            <input type="text" name="title" value="<?php echo $news['title'] ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>关键字</td>
                        <td>
                            <input type="text" name="keywords" value="<?php echo $news['keywords'] ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>作者</td>
                        <td>
                            <input type="text" name="author" value="<?php echo $news['author'] ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>内容</td>
                        <td>
                            <textarea name="content" rows="5" ><?php echo $news['content'] ?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="add">
                            <input type="reset" value="reset">
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
    </html>

      文件上传下载系统

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
    </head>
    <body>
    <form action="flist.php" method="post" enctype="multipart/form-data">
        <input type="file" name="pic" />
        <input type="submit" value="upload">
    </form>
    <?php
        echo "<pre>";
        var_dump($_FILES);
        $typelist = array("image/jpeg","image/jpg","image/png");
        @$up = $_FILES["pic"];
        if(!empty($up)){    
            switch($up["error"]) {
                case 1:
                $info = "";
                break;
            }
            if( !in_array($up["type"], $typelist)) {
                echo "error type";
                return ;
            }
            if( is_uploaded_file($up["tmp_name"]) ) {
                 $upname = iconv('utf-8','gb2312',$up['name']);
                if(move_uploaded_file($up["tmp_name"], "./uploads/".$upname)) {
                    echo "<br>success<br>";
                }else{
                    echo "<br>error<br>";
                }
            }
            echo "</pre>";
    
        }?>
    <?php
        $dir = opendir("uploads");
        $i = 0;
        while($f = readdir($dir)) {
            if($f == "." || $f=="..") {
                continue;
            }
            $i++;
            $name=iconv("gb2312","utf-8",$f);
            echo "{$i}";
            echo "<img src='uploads/{$name}' />";
            echo "<br>";
        }
    ?>
    </body>
    </html>

      

      基于本地文本文件的留言管理系统

      首页添加留言:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <center>
            <form action="doAdd.php" method="POST">
                标题:<input type="text" name="title">
                <br>
                作者:<input type="text" name="author">
                <br><br>
                内容:<textarea name="content">
                </textarea>
                <br>
                <input type="submit" value="提交">
            </form>
        </center>
    </body>
    </html>

      添加留言的业务逻辑:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <?php
            $title = $_POST["title"];
            $author = $_POST["author"];
            $content = $_POST["content"];
            $ip = $_SERVER["REMOTE_ADDR"];
            $addtime = time();
    
            $ly = "{$title}##{$author}##{$content}##{$ip}##{$addtime}@@";
            $ly = $ly.file_get_contents("ly.txt");
            $flag = file_put_contents("ly.txt", $ly);
            if(!$flag) {
                echo "error";
                return;
            }else{
                header('Location: '.'./list.php');
            }
    
        ?>
    </body>
    </html>

      查看留言列表:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    <ul>
        <?php
            $content = file_get_contents("ly.txt");
            $content = rtrim($content, "@@");
            $arr = explode("@@", $content);
    
            foreach($arr as $k=>$list) {
                echo "<li>";
                $con = explode("##", $list);
                echo "标题:{$con[0]}, 作者:{$con[1]}, 内容:{$con[2]} ,ip:{$con[3]}, 时间:".date('Y-m-d',$con[4])."<a href='del.php?id={$k}'>del</a>";
                echo "</li>";
            }
        ?>
    </ul>
    </body>
    </html>

      删除留言:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <?php
            $id = $_GET["id"];
            $con = file_get_contents("ly.txt");
            $con = rtrim($con, "@@");
            $list = explode("@@", $con);
            unset( $list[$id] );
    
            $result = implode("@@", $list);
            file_put_contents("ly.txt", $result);
            header("Location: ./list.php");
        ?>
    </body>
    </html>
    <?php
    
    $arr = array(2,3,1,4);
    foreach( $arr as $i){
        echo $i."
    ";
    }

    作者: NONO
    出处:http://www.cnblogs.com/diligenceday/
    企业网站:http://www.idrwl.com/
    开源博客:http://www.github.com/sqqihao
    QQ:287101329
    微信:18101055830 

    厦门点燃未来网络科技有限公司, 是厦门最好的微信应用, 小程序, 微信网站, 公众号开发公司

  • 相关阅读:
    测试工程师用 Shell 定位 Bug 的正确姿势 | 福利
    基于 JMeter 完成 Dubbo 接口的测试
    门票免费送 | 40+干货议题及重磅讲师阵容全新升级
    Java or Python?测试开发工程师如何选择合适的编程语言?
    测试开发之路 | 希望你们在新家过的更好
    ARMS实践|日志在可观测场景下的应用
    阿里云 ACK 容器服务生产级可观测体系建设实践
    Kruise Rollout v0.2.0 版本发布:支持 Gateway API、StatefulSet 分批发布等能力
    解析 RocketMQ 业务消息“顺序消息”
    1 分钟在 Serverless 上部署现代化 Deno Web 应用
  • 原文地址:https://www.cnblogs.com/diligenceday/p/7227937.html
Copyright © 2020-2023  润新知