标签:
效果:
数据库:
项目结构:
add.php : 添加微博。
conn.php : 数据库配置文件。
delete.php : 删除博客代码。
disinfo.php : 显示微博详细信息。
index.php: 主页。
update.php:编辑微博。
add.php:
<?php //引入数据库连接文件 require(‘conn.php‘); //判断是否提交数据 if(!empty($_POST[‘sub‘])) { //接收数据 $title = $_POST[‘title‘]; $content = $_POST[‘content‘]; //判断时候填写信息 if($title == ‘‘ || $content == ‘‘) { echo ‘请填写完整信息!‘; exit(); } //SQL语句 $sql = "INSERT INTO `tb_article` (`title`, `datetime`, `content`) VALUES (‘$title‘,now(),‘$content‘)"; //执行并判断是否执行成功 if(@mysql_query($sql) or die(‘SQL执行异常!‘)){ echo ‘<script language=javascript>alert("发表成功!");window.location.replace("index.php")</script>‘; } } ?> <html> <head> <meta charset="UTF-8"> <title>添加微博</title> </head> <body> <a href= "index.php">返回首页</a><br/> <form action="add.php" method="POST"> 标题:<input type="text" name="title" /><br/> 内容:<textarea rows="3" name="content"></textarea><br/> <input type="submit" name="sub" value="发表"/> </form> </body> </html>
conn.php:
<?php //连接数据库服务器 @mysql_connect(‘localhost:3306‘,‘root‘,‘123456‘) or die(‘连接数据库服务器失败!‘); //选择数据库 @mysql_select_db(‘db_weibo‘) or die(‘选择数据库失败!‘); //设置编码 @mysql_query("SET NAMES ‘UTF8‘") or die(‘数据库编码错误!‘); ?>
delete.php:
<?php //引入数据库连接 require(‘conn.php‘); //接收index.php页面传递过来的id if(!empty($_GET[‘id‘])) { //接收数据 $id = $_GET[‘id‘]; //SQL语句 $sql = "DELETE FROM `tb_article` WHERE ID = ‘$id‘ limit 1"; //执行 if(@mysql_query($sql) or die(‘SQL语句执行失败!‘)) { echo ‘<script >alert("删除成功!");window.location.replace("index.php")</script>‘; exit(); } } ?>
disinfo.php:
<?php //引入数据库连接文件 require(‘conn.php‘); //接收id if(!empty($_GET[‘id‘])) { //接收数据 $id = $_GET[‘id‘]; //SQL语句 $sql = "SELECT * FROM `tb_article` WHERE id = ‘$id‘ limit 1"; //获取资源句柄 $queryhandle = @mysql_query($sql) or die(‘SQL执行失败!‘); //获取一行数据 $result = mysql_fetch_array($queryhandle,MYSQL_ASSOC); //SQL语句(修改点击量) $sql = "UPDATE `tb_article` SET `click` = `click`+1 WHERE id = ‘$id‘"; //执行并判断是否执行成功 @mysql_query($sql) or die(‘SQL执行异常!‘); } ?> <html> <head> <meta charset="UTF-8"> <title>微博信息内容</title> </head> <body> <a href= "index.php">返回首页</a><br/> 标题:<?php echo $result[‘title‘] ?> <br/> 时间:<?php echo $result[‘datetime‘] ?> <br/> 点击量:<?php echo $result[‘click‘] ?> <br/> 内容:<?php echo $result[‘content‘] ?> <br/> </body> </html>
index.php:
<?php //引入数据库连接文件 require(‘conn.php‘); //获取搜索字段 $keys = $_POST[‘keys‘]; //判断是否有值 if(empty($keys)) { $s = ‘‘; } else { $s = " where `title` like ‘%$keys%‘"; } //获取数据库数据 //SQL语句 $sql = "select * from `tb_article`".$s; //获取资源句柄 $queryhandle = @mysql_query($sql) or die(‘SQL执行失败!‘); //获取总条数 $totalnum = "SELECT COUNT(*) FROM `tb_article`"; $querytotal = mysql_query($totalnum); $totlnum = mysql_fetch_array($querytotal); ?> <html> <head> <meta charset="UTF-8"> <title>添加微博</title> </head> <body> <a href= "add.php">添加微博</a> <hr/> <form action="index.php" method = "post"> <input type="text" name = "keys" /> <input type = "submit" name = "search" value="搜索" /> </form> <hr/> <!-- 循环显示数据库内容 --> <?php while($result = mysql_fetch_array($queryhandle,MYSQL_ASSOC)) { ?> <h3>标题:<a href = "disinfo.php?id=<?php echo $result[‘id‘] ?>"><?php echo $result[‘title‘] ?></a>
| <a href = "update.php?id=<?php echo $result[‘id‘] ?>"> 编辑 </a> | <a href = "delete.php?id=<?php echo $result[‘id‘] ?>"> 删除 </a> |</h3> <h3>时间:<?php echo $result[‘datetime‘] ?> </h3> <h3>点击量:<?php echo $result[‘click‘] ?> </h3> <p><?php echo $result[‘content‘] ?> </p> <hr/> <?php } ?> </body> </html>
update.php:
<?php //引入数据库连接文件 require(‘conn.php‘); //接收id if(!empty($_GET[‘id‘])) { //接收数据 $id = $_GET[‘id‘]; //SQL语句 $sql = "SELECT * FROM `tb_article` WHERE id = ‘$id‘ limit 1"; //获取资源句柄 $queryhandle = @mysql_query($sql) or die(‘SQL执行失败!‘); //获取一行数据 $result = mysql_fetch_array($queryhandle,MYSQL_ASSOC); } //判断是否提交数据 if(!empty($_POST[‘sub‘])) { //接收数据 $hid = $_POST[‘hid‘]; $title = $_POST[‘title‘]; $content = $_POST[‘content‘]; //判断时候填写信息 if($title == ‘‘ || $content == ‘‘) { echo ‘请填写完整信息!‘; exit(); } //SQL语句 $sql = "UPDATE `tb_article` SET `title` = ‘$title‘, `content` = ‘$content‘ WHERE id = ‘$hid‘"; //执行并判断是否执行成功 if(@mysql_query($sql) or die(‘SQL执行异常!‘)){ echo ‘<script language=javascript>alert("修改成功!");window.location.replace("index.php")</script>‘; } } ?> <html> <head> <meta charset="UTF-8"> <title>添加微博</title> </head> <body> <a href= "index.php">返回首页</a><br/> <form action="update.php" method="POST"> <input type = "hidden" name = "hid" value = "<?php echo $result[‘id‘] ?>"> 标题:<input type="text" name="title" value="<?php echo $result[‘title‘] ?>" /><br/> 内容:<textarea rows="3" name="content"><?php echo $result[‘content‘] ?></textarea><br/> <input type="submit" name="sub" value="发表"/> </form> </body> </html>
标签:
原文地址:http://www.cnblogs.com/KTblog/p/5058326.html