效果:javascript
數據庫:php
項目結構:html
add.php : 添加微博。java
conn.php : 數據庫配置文件。mysql
delete.php : 刪除博客代碼。sql
disinfo.php : 顯示微博詳細信息。數據庫
index.php: 主頁。服務器
update.php:編輯微博。post
add.php:fetch
<?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>