1、明确需求
- 数据库:有关新闻的编号、标题、发布时间、发布人、是否置顶、内容属性,其中编号用自增量
-
功能:
- 实现在网页上通过PHP文件对数据库进行操作
- 实现新闻的信息添加
- 对新闻进行修改功能
- 对新闻进行删除功能
- 对新闻进行查找功能
代码:
2、HTML
2.1、news_add.html:
进行新闻添加页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="news_add.php" method="post" >
<table>
<!-- 标题开始 -->
<tr>
<td width="10%" > 标题</td>
<td width="40%" ><input type="text" id='title' name="title"></td>
</tr>
<!-- 标题结束 -->
<!-- 是否置顶 -->
<tr>
<td width="10%" >是否置顶</td>
<td width="10%">
<input type="radio" name="isTop" value="1"/>是
<input type="radio" name="isTop" checked="checked" value="0"/>否
</td>
</tr>
<!-- 是否置顶 -->
<tr>
<td width="10%" >发布人</td>
<td width="40%" ><input type="text" id='name' name="name"></td>
</tr>
<!-- 内容 -->
<tr>
<td width="10%" >内容</td>
<td colspan="3">
<textarea name="neirong" id="neirong" rows="15" style="width:100%;"></textarea>
</td>
</tr>
<!-- 内容 -->
<tr>
<td ></td>
<td colspan="3">
<input type="submit" class="btn btn-primary" value="保存"/>
<input type="button" class="btn btn-success" name="backid" id="backid" value="返回"/>
<font color="red"></font>
</td>
</tr>
</table>
</form>
</body>
</html>
2.2、news_index.html
新闻的首页展示,具有查询全部新闻的功能
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新闻管理首页</title>
</head>
<body>
<form action="news_seleect.php" method="post">
标题:<input name="title" value=""></input>
<button type="submit" name="btn">查询</button>
<button type="button" onclick="location='news_add.html'">添加</button>
</form>
<table>
<thead>
<tr>
<th>编号</th>
<th>标题</th>
<th>发布时间</th>
<th>发布人</th>
<th>是否置顶</th>.
<th>操作</th>
</tr>
</thead>
<?php for ($i=0; $i < $num; $i++): ?>
<tr>
<td>|<?php echo $news[$i]['id']; ?> </td>
<td>|<?php echo $news[$i]['title']; ?> </td>
<td>|<?php echo date('Y-m-d H:i:s',$news[$i]['public_time']); ?> </td>
<td>|<?php echo $news[$i]['name']; ?> </td>
<td>|<?php echo ($news[$i]['isTop']==1) ? '是':'否' ; ?> </td>
<td style="text-align:center;">
|<a href="news_edit.php?id= <?php echo $news[$i]['id']; ?>">修改</a> |
<a href="news_delete.php?id= <?php echo $news[$i]['id']; ?>" onclick="return confirm('是否确定删除当前新闻?');">删除</a>
</td>
</tr>
<?php endfor ?>
</table>
</body>
</html>
2.3、news_edit.html
对新闻进行修改的页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改新闻</title>
</head>
<body>
<form action="news_update.php" method="post" >
<!-- 这一行隐藏的用户看不到,用来传id的 -->
<input type="hidden" name="id" value="<?php echo $new['id'];?>"/>
<table>
<!-- 标题开始 -->
<tr>
<td width="10%" > 标题</td>
<td width="40%" ><input type="text" id='title' name="title" value="<?php echo $new['title']; ?>"></td>
</tr>
<!-- 标题结束 -->
<!-- 是否置顶 -->
<tr>
<td width="10%" >是否置顶</td>
<td width="10%">
<input type="radio" name="isTop" value="1" <?php echo $new['isTop']==1?'checked="checked"':''; ?>/>是
<input type="radio" name="isTop" value="0" <?php echo $new['isTop']==0?'checked="checked"':''; ?>/>否
</td>
</tr>
<!-- 是否置顶 -->
<tr>
<td width="10%" >发布人</td>
<td width="40%" ><input type="text" id='name' name="name" value="<?php echo $new['name']; ?>"></td>
</tr>
<!-- 内容 -->
<tr>
<td width="10%" >内容</td>
<td colspan="3">
<textarea name="neirong" id="neirong" rows="15" style="width:100%;" ><?php echo $new['neirong'] ?></textarea>
</td>
</tr>
<!-- 内容 -->
<tr>
<td ></td>
<td colspan="3">
<input type="submit" class="btn btn-primary" value="保存"/>
<input type="button" class="btn btn-success" name="backid" id="backid" value="返回"/>
<font color="red"></font>
</td>
</tr>
</table>
</form>
</body>
</html>
2.4、news_seleect.html
新闻查询页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询结果</title>
</head>
<body>
<form action="news_seleect.php" method="post">
标题:<input name="title" value=""></input>
<button type="submit" name="btn">查询</button>
<button type="button" onclick="location='news_add.html'">添加</button>
<a href="news_index.php">首页</a>
</form>
<table>
<thead>
<tr>
<th>编号</th>
<th>标题</th>
<th>发布时间</th>
<th>发布人</th>
<th>是否置顶</th>.
<th>操作</th>
</tr>
</thead>
<?php for ($i=0; $i < $num; $i++): ?>
<tr>
<td>|<?php echo $news[$i]['id']; ?> </td>
<td>|<?php echo $news[$i]['title']; ?> </td>
<td>|<?php echo date('Y-m-d H:i:s',$news[$i]['public_time']); ?> </td>
<td>|<?php echo $news[$i]['name']; ?> </td>
<td>|<?php echo ($news[$i]['isTop']==1) ? '是':'否' ; ?> </td>
<td style="text-align:center;">
|<a href="news_edit.php?id= <?php echo $news[$i]['id']; ?>">修改</a> |
<a href="news_delete.php?id= <?php echo $news[$i]['id']; ?>" onclick="return confirm('是否确定删除当前新闻?');">删除</a>
</td>
</tr>
<?php endfor ?>
</table>
</body>
</html>
3、PHP
3.1、news_add.php
新闻添加功能
<?php
header('Content-type:text/html;charset=utf-8');
//接受用户数据
//var_dump($_POST);
//isset每一个都判断用户填写数据是否为空
//trim去掉空格
$title=isset($_POST['title'])? trim($_POST['title']) : '';
$isTop=isset($_POST['isTop'])? $_POST['isTop'] : 0;
$neirong=isset($_POST['neirong'])? trim($_POST['neirong']) : '';
$name=isset($_POST['name']) ? trim($_POST['name']) :'';
//验证标题和内容不能为空
if (empty($title)||empty($neirong) ) {
//提示同时回到提交页
header('Refresh:3;url=news_add.html'); //header前不能有输出,header:refresh不会阻止脚本执行
//标题和内容至少有一个为空//阻止脚本继续执行
exit('标题和内容都不能为空!');
}
//数据库入库
//MYSQL前提处理PHP文件
include_once 'news_function.php';
//执行SQL指令
$time=time();
//插入数据
$sql = "insert into news values(null,'{$title}',{$isTop},'{$neirong}','{$name}',{$time})";
$insert_id=error_test($sql);
//echo $title.'添加成功';
header('Refresh:3;url=news_index.php');
echo $title.'添加成功';
?>
3.2、news_delete.php
新闻删除功能
<?php
// 删除数据库的某一行
header('Content-type:text/html;charset=utf-8');
include_once 'news_function.php';
$id=isset($_REQUEST)?(integer)$_REQUEST['id']:0;
if ($id==0) {
header('Refresh:3;url=news_index.php');
echo '该新闻已近不存在了';
exit;
}
if(error_test("delete from news where id ={$id}")){
header('Refresh:3;url=news_index.php');
echo'删除成功';
}
?>
3.3、news_edit.php
新闻更新
<?php
//修改数据
header('Content-type:text/html;charset=utf-8');
include_once 'news_function.php';
$id=isset($_REQUEST)?(integer)$_REQUEST['id']:0;
if ($id==0) {
header('Refresh:3;url=news_index.php');
echo '该新闻已近不存在了';
exit;
}
$sql="select * from news where id={$id}";
$res=error_test($sql);
$new=mysql_fetch_assoc($res);//得到的是一个数组
include_once 'news_edit.html';
?>
3.4、news_function.php
PHP连接数据库基本操作
<?php
//实现PHP连接数据库的代码处理
header('Content-type:text/html;charset=utf-8');
//连接数据库
mysql_connect('localhost:3306','root','root');
//设定字符集
mysql_query("set names utf8");
//选择数据库
mysql_query("use mydatabase2");
/*
* 功能:参数为SQL指令,检测该SQL指令是否错误,错误则报错,正确则返回资源集
* string $sql,MYSQL指令
*/
function error_test($sql){
$res=mysql_query($sql);
//错误判定
if(!$res){
//代表结果为false
echo 'SQL指定错误,错误代号为:'.mysql_errno().'<br>';
echo 'SQL指令错误,错误信息为:'.mysql_error();
exit;//错误就终止代码
}
return $res;
}
?>
3.5、news_index.php
首页展示功能
<?php
//查询数据的后期管理
header('Content-type:text/html;charset=utf-8');
//获SQL取前置条件
include_once 'news_function.php';
//获取数据
$sql="select * from news";
$res=error_test($sql);
//把所有结果都放在一个数组$news里面,然后$news[0]是一个新闻所有信息,$news[1]又是一个新闻的所有信息
//获取结果集行数
$num=mysql_num_rows($res);
$news[]=array();//建立一个空数组
for ($i=0; $i <$num ; $i++) {
$news[$i]=mysql_fetch_assoc($res);//得到的是一个数组
}
include_once'news_index.html';//当网站输入news.index.php时,会自动转进去news.index.html,而news.index.html里面的数据会来源于news.index.php,因为是php跳转到html的,所有的数据都还保留着
?>
3.6、news_seleect.php
新闻查询功能
<?php
//查询数据
header('Content-type:text/html;charset=utf-8');
include_once 'news_function.php';
$as=$_REQUEST['title'];
//获取数据
$sql="select * from news where title like '%{$as}%';";
$res=error_test($sql);
//把所有结果都放在一个数组$news里面,然后$news[0]是一个新闻所有信息,$news[1]又是一个新闻的所有信息
//获取结果集行数
$num=mysql_num_rows($res);
$news[]=array();//建立一个空数组
for ($i=0; $i <$num ; $i++) {
$news[$i]=mysql_fetch_assoc($res);//得到的是一个数组
}
include_once 'news_seleect.html';
?>
3.7、news_update.php
数据更新功能
<?php
//更新数据
header('Content-type:text/html;charset=utf-8');
include_once 'news_function.php';
//var_dump($_REQUEST);
$title=$_REQUEST['title'];
//var_dump($title);
$isTop=$_REQUEST['isTop'];
$name=$_REQUEST['name'];
$neirong=$_REQUEST['neirong'];
$time=time();
$id=$_REQUEST['id'];
//验证标题和内容不能为空
if (empty($title)||empty($neirong) ) {
//提示同时回到提交页
header('Refresh:3;url=news_add.html'); //header前不能有输出,header:refresh不会阻止脚本执行
//标题和内容至少有一个为空//阻止脚本继续执行
exit('标题和内容都不能为空!');
}
//更新数据的SQL指令
$sql = "update news set title='{$title}',isTop={$isTop},name='{$name}',neirong='{$neirong}' where id={$id} ";
//更新数据
$delete_id=error_test($sql);
header('Refresh:3;url=news_index.php');
echo $title.'修改成功';
?>