通过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
//连接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
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);
?>
添加内容
if(!$rs){
echo "没有相关内容!";
}
//没有实现分页导航功能
while($rs){
?>
标题:|编辑|删除
内容...... |查看详细内容|
$rs = mysql_fetch_array($query);
}
?>
add.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);
}
?>
查看内容
edit.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 "更新按钮提交成功!";
}
?>
delete.php
include("conn.php");
if(!empty($_GET['id'])){
mysql_query("delete from micro_blog where id =".$_GET['id']);
} else {
echo "参数引入失败!";
}
?>
返回主页面
view.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 "参数引入失败!";
}
?>
返回主页面

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment