编写 delete_post.php 实现删除文章的功能
编写 update_post.php 实现修改文章的功能
在 list_post.php 中加入指向 delete_post.php 和 update_post.php 的链接(如下图所示,思考如何实现)PHP code
?
1
//config.php
<?php
$db_host = 'localhost';
$db_database='blog';
$db_username='username';
$db_password='pass';
?>
//list_post.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
    <title>Simple Blog System—发表文章</title>
  </head>
  <body>
  <h1>Simple Blog System—文章列表</h1>
<?php
  require('config.php');  @ $db = new mysqli($db_host,  $db_username, $db_password, $db_database);  if (mysqli_connect_errno()) {
     echo '错误: 无法连接到数据库. 请稍后再次重试.';
     exit;
  }
    // 设置字符集
  $db->query("SET NAMES utf8");  $query = "select * from post";
  $result = $db->query($query);  $num_results = $result->num_rows;  echo '<p>共有文章: '.$num_results.' 篇</p>';  for ($i=0; $i <$num_results; $i++)
  {
     $row = $result->fetch_assoc();
     echo '<h2>'.($i+1).'.  ' . htmlspecialchars(stripslashes($row['post_title'])) . "</h2>\n";
     echo '<p>发表时间:' . htmlspecialchars(stripslashes($row['post_date'])) . "</p>\n";
     echo '<p>'.nl2br(htmlspecialchars(stripslashes($row['post_content']))).'</p>';
  }  $result->free();
  $db->close();
?>
</body>
</html>
//new_post.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
    <title>Simple Blog System—发表文章</title>
  </head>
  <body>
  <h1>发表博客文章</h1>
<?php
  require('config.php');
  date_default_timezone_set('Asia/Shanghai');
  
  $title=$_POST['title'];
  $content=$_POST['content'];  if (!$title || !$content)
  {
     echo '你未输入文章的标题或正文.<br />'
          .'请退回再次重试.';
     exit;
  }
  if (!get_magic_quotes_gpc()) {
    $title = addslashes($title);
    $body = addslashes($content);
  }  @ $db = new mysqli($db_host,  $db_username, $db_password, $db_database);  if (mysqli_connect_errno()) {
     echo '错误: 无法连接到数据库. 请稍后再次重试.';
     exit;
  }
  // 设置字符集
  $db->query("SET NAMES utf8");  $query = "insert into post values('', '" . $title . "', '"  . $content . "','" . date('Y-m-d H:i:s') . "')";
  echo '<pre>' . $query . '</pre>' ;  $result = $db->query($query);
  if ($result)
      echo  $db->affected_rows.' 条记录被插入数据库中';
  else
      echo '插入记录错误,请检查程序代码';  $db->close();
?>
</body>
</html>
//new_post.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
    <title>Simple Blog System—发表文章</title>
  </head>
  <body>
  <h1>发表博客文章</h1>
  <form action="new_post.php" method="post">
    <p>标题:</p>
    <p><input type="text" name="title" maxlength="60" size="30" /></p>
    <p>正文:</p>
    <p><textarea name = "content" rows="10" cols="80"></textarea></p>
    <p><input type="submit" value="提交" /></p>
  </form>
  </body>
</html>
//blog.sql
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";-- 
-- 数据库: `blog`
-- 
CREATE DATABASE `blog` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `blog`;-- ---------------------------------------------------------- 
-- 表的结构 `post`
-- CREATE TABLE `post` (
  `id` int(11) NOT NULL auto_increment,
  `post_title` text character set utf8,
  `post_content` text character set utf8,
  `post_date` datetime,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;-- 
-- 导出表中的数据 `post`
-- INSERT INTO `post` (`id`, `post_title`, `post_content`, `post_date`) VALUES 
(1, '使至塞上', '单车欲问边,属国过居延。\r\n征蓬出汉塞,归雁入胡天。\r\n大漠孤烟直,长河落日圆。\r\n萧关逢候骑,都护在燕然。', '2009-03-22 15:28:10'),
(2, '长相思', '山一程\r\n水一程\r\n身向榆关那畔行\r\n夜深千帐灯\r\n  \r\n风一更\r\n雪一更\r\n聒碎乡心梦不成\r\n故园无此声', '2009-03-22 15:29:20'),
(3, '如梦令', '万帐穹庐人醉\r\n星影摇摇欲坠\r\n\r\n归梦隔狼河\r\n又被河声搅碎\r\n\r\n还睡\r\n还睡\r\n解道醒来无味', '2009-03-22 15:29:30'),
(4, '梦江南', '昏鸦尽\r\n小立恨因谁\r\n急雪乍翻香阁絮\r\n轻风吹到胆瓶梅\r\n心字已成灰', '2009-03-22 15:32:27');
编写一个登录页面,创建一个 user 表保存登录信息(username, password etc.),用户登录后才能显示之前的修改和删除链接;如果是未登录游客,则隐藏操作链接。
最终的系统可以创建,查看,修改,删除文章,也就是 CRUD 四大操作,外带登录功能(用 session 实现)主要是为了求教设计思路或者方法,源代码也可以