通过php+mysql 实现的简易blog,可以实现增删改查。效果如下图:
一、数据库及表结构
数据库:test
表:micro_blog(仅仅有一个表)字段:id,title,date,content,hits
表结构如下:
CREATE TABLE `micro_blog` (
`id` int(20) unsigned NOT NULL AUTO_INCREMENT,
`title` text NOT NULL ,
`content` longtext NOT NULL,
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`hits` int(20) DEFAULT 0,
PRIMARY KEY (`ID`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
二、文件
文件详细描述
文件 描述
default.php 默认主页。显示博文与操作连接。
add.php 添加新博文的功能模块。
edit.php 对已经添加过的博文进行修改操作。
delete.php 删除博文模块。
view.php 显示博文的详细信息(标题|添加日期|浏览次数|内容)。
conn.php 链接数据库操作。在其它文件中被引用。
conn.php
<?php
//连接MySql数据库服务
$conn = @mysql_connect("localhost:3306","root","www.361way.com") or die("连接数据库服务器失败!");
//连接ly_php_base数据库
@mysql_select_db("test",$conn) or die("未能连接到数据库!");
mysql_query("SET NAMES 'UTF8'");
?>
注:后面的set names utf8,如果不执行,会出现插入数据库中的汉字会变成乱码。
default.php
<?php
include("conn.php");
//搜索关键字的管理
if(!empty($_GET['keys'])){
$keys = "WHERE title like '%".$_GET['keys']."%'";
} else {
$keys = "";
}
$sql = "SELECT * FROM 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 micro_blog VALUES(NUll,'$title','$content',now(),0)";
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 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
include("conn.php");
if(!empty($_GET['id'])){
mysql_query("delete from micro_blog 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>
view.php
<?php
include("conn.php");
if(!empty($_GET['id'])){
$sql = "SELECT * FROM micro_blog WHERE id = ".$_GET['id'];
$rc = mysql_fetch_array(mysql_query($sql));
mysql_query("UPDATE 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>