新建立一个文件listnews.php,内容如下:
-
< ?php
-
require_once ( '../inc/config.php' );
-
//包含配置文件
-
?>
-
<html>
-
<head></head>
-
<title>新闻管理</title>
-
<body>
-
<div class= "newstitle"></div>
-
<div class= "newslist">
-
< ?php
-
//这里显示新闻列表
-
$page = $_GET [ 'page' ];
-
-
if ( $page < 1 ) $page = 1;
-
-
//获取当前页面数
-
$per = 10;
-
//每页显示数量
-
$begin = ( $page - 1 ) * $per;
-
-
$pre = $page -1;
-
//上一页
-
$next = $page + 1;
-
//下一页
-
//反正所有的操作都会用到mysql连接,所以我们先建立连接
-
-
$sql = "SELECT * FROM ".NEWS_BASE. " WHERE 1 ORDER BY news_id limit ". $begin. ",". $per;
-
-
//查询数据库
-
-
echo "<table border='1'><tr>";
-
echo "<td>新闻时间</td><td>新闻标题</td>";
-
echo "<td>操作</td></tr><tr>\n";
-
-
//循环取出数据,关于mysql的函数请参考php手册
-
//然后循环表格
-
echo "</tr><tr>";
-
echo "</tr>";
-
//上面的按钮会触发javascript事件
-
//所以下面我将顺便写一下js
-
//让大家熟悉一下
-
}
-
echo "";
-
?>
-
<a href= "?page=<?php echo $pre;?>">上页</a>
-
<a href= "?page=<?php echo $next;?>">下页</a>
-
</div>
-
<script language= "javascript">
-
function mod (id ) {
-
//修改的函数
-
//我们将这个带到editnews.php?id=id上去
-
window.location = "editnews.php?id="+id+ "&page=< ?php echo $page;?>";
-
}
-
function del (id ) {
-
//修改的函数
-
//我们将这个带到do.php?id=id&del=yes上去
-
window.location = "do.php?id="+id + "&del=yes&page=< ?php echo $page;?>";
-
}
-
</script>
-
</body>
-
</html>
现在开始便写如何删除的页面,打开do.php,添加下面的代码
-
if ( $_GET [ 'del' ] == 'yes' ) {
-
$sql = "DELETE FROM ".NEWS_BASE. " WHERE news_id='". $_GET [ 'id' ]. "'";
-
//删除之后返回到原来的那个页面
-
}
现在开始编写editnews.php了,先从数据库中取出数据,然后用sql update内容即可,代码如下:
-
< ?php
-
require_once ( '../inc/config.php' );
-
$sql = "SELECT * FROM ".NEWS_BASE. " WHERE news_id='". $_GET [ 'id' ]. "'";
-
?>
-
<form method= "post" action= "do.php">
-
<input type= "hidden" id= "action" name= "action" value= "modnews" />
-
<br />
-
<input type= "hidden" name= "news_id" value= "<?php echo $obj-/>news_id?>" />
-
<input type= "hidden" name= "page" value= "<?php echo $_GET['page']?/>" />
-
<!-- hidden隐藏域的作用就不用介绍了吧,上次介绍过哈 -->
-
<label for= "news_title">新闻标题</label>
-
<input name= "news_title" id= "news_title"
-
value= "<?php echo $obj-/>news_title;?>" size= "60" type= "text" />
-
<br />
-
<label for= "news_text">新闻内容</label>
-
<textarea type= "text" name= "news_text" id= "news_text" rows= "10" cols= "50">
-
</textarea>
-
<br />
-
<label for= "author">新闻作者</label>
-
<input name= "author" id= "author" size= "60" type= "text"
-
value= "<?php echo $obj-/>author;?>" /><br />
-
<label for= "news_date">发布时间</label>
-
<input name= "news_date" id= "news_date" size= "60" type= "text"
-
value= "<?php echo $obj-/>news_date;?>" /><br />
-
<input id= "sbbt" value= "提交" type= "submit" />
-
<input id= "resetbt" value= "重置" type= "reset" />
-
</form>
然后我们要在对应的do.php中添加相应的处理操作,代码如下
-
if ( "modnews" == $action ) {
-
//如果是修改新闻,则在此处进行处理
-
$sql = "UPDATE ".NEWS_BASE. " set
-
news_title='". $_POST [ 'news_title' ]. "',
-
news_text='". $_POST [ 'news_text' ]. "',
-
news_date='". $_POST [ 'news_date' ]. "',
-
author='". $_POST [ 'author' ]. "' WHERE news_id=". $_POST [ 'news_id' ]. "";
-
}
当然,我们在实际的处理中,必须对用户的输入进行过滤,防止恶意的输入,比如用这样的函数过滤掉html标签$a = htmlspecialchars($_POST[’a']);
好了,今天就介绍到这里,一个基本的新闻管理系统基本完成了。