zoukankan      html  css  js  c++  java
  • 利用PHP/MYSQL实现的简易微型博客(转)

    数据库:ly_php_base

    表:ly_micro_blog(仅仅有一个表)字段:id,title,date,content,hits

    文件:

    文件 描述
    default.php 默认主页。显示博文与操作连接。
    add.php 添加新博文的功能模块。
    edit.php 对已经添加过的博文进行修改操作。
    delete.php 删除博文模块。
    view.php 显示博文的详细信息(标题|添加日期|浏览次数|内容)。
    conn.php 链接数据库操作。在其它文件中被引用。

    conn.php

    <?php  
    /** 
     *ly_micro_blog 
     *ID|TITLE|CONTENT|DATE 
     **/  
    ?>  
    <?php  
    //连接MySql数据库服务  
    $conn = @mysql_connect("localhost:3306","root","228580") or die("连接数据库服务器失败!");  
    //连接ly_php_base数据库  
    @mysql_select_db("ly_php_base",$conn) or die("未能连接到数据库!");  
    //mysql_query("SET NAMES 'GBK'");  
    ?>  
    <?php  
    include("conn.php");  
    //搜索关键字的管理  
    if(!empty($_GET['keys'])){  
        $keys = "WHERE title like '%".$_GET['keys']."%'";  
    } else {  
        $keys = "";  
    }  
    $sql = "SELECT * FROM ly_micro_blog ".$keys." ORDER BY id DESC LIMIT 10";  
    $query = mysql_query($sql);  
    $rs = mysql_fetch_array($query);  
    ?>  
    <html>  
    <head>  
    <title>我的微博客主页</title>  
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>  
    </head>  
    <body>  
    <a href="add.php">添加内容</a>  
    <form action="" method="get">  
      <input type="text" name="keys"/>  
      <input type="submit" name="submit" value="内容搜索"/>  
    </form>  
    <hr color="#FF9900" size="3" />  
    <?php  
    if(!$rs){  
        echo "没有相关内容!";  
    }  
    //没有实现分页导航功能  
    while($rs){  
    ?>  
    <h2>标题:<?php echo $rs['title'];?>|<a href="edit.php?id=<?php echo $rs['id'];?>">编辑</a>|<a href="delete.php?id=<?php echo $rs['id'];?>">删除</a></h2>  
    <li>日期:<?php echo $rs['date'];?></li>  
    <p>内容<?php echo iconv_substr($rs['content'],0,50,"UTF-8");?>...... <a href="view.php?id=<?php echo $rs['id'];?>">|查看详细内容|</a></p>  
    <hr color="#0033FF" size="5" />  
    <?php  
        $rs = mysql_fetch_array($query);  
    }  
    ?>  
    </body>  
    </html>

    add.php

    <?php  
    //引入连接数据库文件  
    include("conn.php");  
      
    if(!empty($_POST['submit'])){  
        $title = $_POST['title'];  
        $content = $_POST['content'];  
        $sql = "INSERT INTO ly_micro_blog VALUES(NULL,'$title','$content',now())";  
        mysql_query($sql);  
    }  
    ?>  
    <!DOCTYPE HTML>  
    <html>  
    <head>  
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>  
    <title>发布微博页面</title>  
    </head>  
    <body>  
    <a href="default.php">查看内容</a>  
    <hr color="#0033CC" size="3px"/>  
    <form action="add.php" method="post">  
      标题:  
      <input type="text" name="title"/>  
      <br />  
      内容:  
      <textarea rows="5" cols="50" name="content"></textarea>  
      <br />  
      <input type="submit" name="submit" value="提交"/>  
      <br />  
    </form>  
    </body>  
    </html>  

    edit.php

    <?php   
    include("conn.php");  
    if(!empty($_GET['id'])){  
        $id = $_GET['id'];  
        $sql = "select * from ly_micro_blog where id = ".$_GET['id'];  
        $query = mysql_query($sql);   
        $rc = mysql_fetch_array($query);  
    }  
      
    if(!empty($_POST['update'])){  
        echo "更新按钮提交成功!";  
    }  
    ?>  
      
    <!DOCTYPE HTML>  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>编辑页面</title>  
    </head>  
      
    <body>  
    <form action="edit.php?id=<?php echo $id;?>" method="post">  
      标题:  
      <input type="text" name="title" value="<?php echo $rc['title'];?>"/>  
      <br />  
      内容:  
      <textarea rows="5" cols="50" name="content"><?php echo $rc['content'];?></textarea>  
      <br />  
      <input type="submit" name="update" value="更新"/>  
      <br />  
    </form>  
    </body>  
    </html>  

    delete.php

    ?php  
    /** 
     *ly_micro_blog 
     *ID|TITLE|CONTENT|DATE 
     **/  
    ?>  
    <?php  
    //连接MySql数据库服务  
    $conn = @mysql_connect("localhost:3306","root","228580") or die("连接数据库服务器失败!");  
    //连接ly_php_base数据库  
    @mysql_select_db("ly_php_base",$conn) or die("未能连接到数据库!");  
    //mysql_query("SET NAMES 'GBK'");  
    ?>  

    view.php

    <?php   
    include("conn.php");  
      
    if(!empty($_GET['id'])){  
        $sql = "SELECT * FROM ly_micro_blog WHERE id = ".$_GET['id'];  
        $rc = mysql_fetch_array(mysql_query($sql));  
        mysql_query("UPDATE ly_micro_blog SET hits = hits + 1 WHERE id = ".$_GET['id']);  
    } else {  
        echo "参数引入失败!";  
    }  
    ?>  
      
    <!DOCTYPE HTML>  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title><?php echo $rc['title'];?>|我的微博客</title>  
    </head>  
      
    <body>  
    <a href="default.php">返回主页面</a>  
    <hr color="#00FFFF" size="5px"/>  
    <h2><?php echo $rc['title'];?>  
      <hr color="#006699" size="3px">  
    </h2>  
    <li><?php echo "日期:".$rc['date']."|浏览次数:".$rc['hits'];?></li>  
    <p><?php echo $rc['content'];?></p>  
    </body>  
    </html>  

    转自http://blog.csdn.net/sunshinedabby/article/details/7848125

  • 相关阅读:
    Redis常用操作命令
    redis-sentinel.conf配置项详解
    Kafka常用命令
    go modules的使用姿势
    GO语言密码加解密(bcrypt)
    ssh-copy-id 秘钥分发报错
    k8s 命令提示
    算法与数据结构(持续更新)
    【spring】 @PostConstruct注解
    Spring Boot 整合Redis
  • 原文地址:https://www.cnblogs.com/bestsamcn/p/4824286.html
Copyright © 2011-2022 走看看