PHP+MySQL实现简易博客
通过php+mysql 实现的简易blog,可以实现增删改查。效果如下图:
一、数据库及表结构
数据库:test
表:micro_blog(仅仅有一个表)字段:id,title,date,content,hits 表结构如下:
1CREATE TABLE `micro_blog` (
2 `id` int(20) unsigned NOT NULL AUTO_INCREMENT,
3 `title` text NOT NULL ,
4 `content` longtext NOT NULL,
5 `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
6 `hits` int(20) DEFAULT 0,
7 PRIMARY KEY (`ID`)
8)ENGINE=MyISAM DEFAULT CHARSET=utf8;
二、文件
文件详细描述
文件 | 描述 |
---|---|
default.php | 默认主页。显示博文与操作连接。 |
add.php | 添加新博文的功能模块。 |
edit.php | 对已经添加过的博文进行修改操作。 |
delete.php | 删除博文模块。 |
view.php | 显示博文的详细信息(标题|添加日期|浏览次数|内容)。 |
conn.php | 链接数据库操作。在其它文件中被引用。 |
conn.php
1<?php
2//连接MySql数据库服务
3$conn = @mysql_connect("localhost:3306","root","www.361way.com") or die("连接数据库服务器失败!");
4//连接ly_php_base数据库
5@mysql_select_db("test",$conn) or die("未能连接到数据库!");
6mysql_query("SET NAMES 'UTF8'");
7?>
注:后面的set names utf8,如果不执行,会出现插入数据库中的汉字会变成乱码。
default.php
1<?php
2include("conn.php");
3//搜索关键字的管理
4if(!empty($_GET['keys'])){
5 $keys = "WHERE title like '%".$_GET['keys']."%'";
6} else {
7 $keys = "";
8}
9$sql = "SELECT * FROM micro_blog ".$keys." ORDER BY id DESC LIMIT 10";
10$query = mysql_query($sql);
11$rs = mysql_fetch_array($query);
12?>
13<html>
14<head>
15<title>我的微博客主页</title>
16<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
17</head>
18<body>
19<a href="add.php">添加内容</a>
20<form action="" method="get">
21 <input type="text" name="keys"/>
22 <input type="submit" name="submit" value="内容搜索"/>
23</form>
24<hr color="#FF9900" size="3" />
25<?php
26if(!$rs){
27 echo "没有相关内容!";
28}
29//没有实现分页导航功能
30while($rs){
31?>
32<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>
33<li>日期:<?php echo $rs['date'];?></li>
34<p>内容<?php echo iconv_substr($rs['content'],0,50,"UTF-8");?>...... <a href="view.php?id=<?php echo $rs['id'];?>">|查看详细内容|</a></p>
35<hr color="#0033FF" size="5" />
36<?php
37 $rs = mysql_fetch_array($query);
38}
39?>
40</body>
41</html>
add.php
1<?php
2//引入连接数据库文件
3include("conn.php");
4if(!empty($_POST['submit'])){
5 $title = $_POST['title'];
6 $content = $_POST['content'];
7 $sql = "INSERT INTO micro_blog VALUES(NUll,'$title','$content',now(),0)";
8 mysql_query($sql);
9}
10?>
11
12<html>
13<head>
14<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
15<title>发布微博页面</title>
16</head>
17<body>
18<a href="default.php">查看内容</a>
19<hr color="#0033CC" size="3px"/>
20<form action="add.php" method="post">
21 标题:
22 <input type="text" name="title"/>
23 <br />
24 内容:
25 <textarea rows="5" cols="50" name="content"></textarea>
26 <br />
27 <input type="submit" name="submit" value="提交"/>
28 <br />
29</form>
30</body>
31</html>
edit.php
1<?php
2include("conn.php");
3if(!empty($_GET['id'])){
4 $id = $_GET['id'];
5 $sql = "select * from micro_blog where id = ".$_GET['id'];
6 $query = mysql_query($sql);
7 $rc = mysql_fetch_array($query);
8}
9if(!empty($_POST['update'])){
10 echo "更新按钮提交成功!";
11}
12?>
13
14<html>
15<head>
16<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
17<title>编辑页面</title>
18</head>
19<body>
20<form action="edit.php?id=<?php echo $id;?>" method="post">
21 标题:
22 <input type="text" name="title" value="<?php echo $rc['title'];?>"/>
23 <br />
24 内容:
25 <textarea rows="5" cols="50" name="content"><?php echo $rc['content'];?></textarea>
26 <br />
27 <input type="submit" name="update" value="更新"/>
28 <br />
29</form>
30</body>
31</html>
delete.php
1<?php
2include("conn.php");
3if(!empty($_GET['id'])){
4 mysql_query("delete from micro_blog where id =".$_GET['id']);
5} else {
6 echo "参数引入失败!";
7}
8?>
9
10<html>
11<head>
12<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
13<title><?php echo $rc['title'];?>|我的微博客</title>
14</head>
15<body>
16<a href="default.php">返回主页面</a>
17<hr color="#00FFFF" size="5px"/>
18<h2><?php echo $rc['title'];?>
19 <hr color="#006699" size="3px">
20</h2>
21<li><?php echo "日期:".$rc['date']."|浏览次数:".$rc['hits'];?></li>
22<p><?php echo $rc['content'];?></p>
23</body>
24</html>
view.php
1<?php
2include("conn.php");
3if(!empty($_GET['id'])){
4 $sql = "SELECT * FROM micro_blog WHERE id = ".$_GET['id'];
5 $rc = mysql_fetch_array(mysql_query($sql));
6 mysql_query("UPDATE micro_blog SET hits = hits + 1 WHERE id = ".$_GET['id']);
7} else {
8 echo "参数引入失败!";
9}
10?>
11
12<html>
13<head>
14<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
15<title><?php echo $rc['title'];?>|我的微博客</title>
16</head>
17<body>
18<a href="default.php">返回主页面</a>
19<hr color="#00FFFF" size="5px"/>
20<h2><?php echo $rc['title'];?>
21 <hr color="#006699" size="3px">
22</h2>
23<li><?php echo "日期:".$rc['date']."|浏览次数:".$rc['hits'];?></li>
24<p><?php echo $rc['content'];?></p>
25</body>
26</html>