abstract:該案例是新聞列表的后臺(tái)管理,涉及數(shù)據(jù)的增刪查改(CURD)操作。創(chuàng)建一個(gè)新聞列表的數(shù)據(jù)庫(kù):1. 查詢(xún)數(shù)據(jù)庫(kù)1.1. 創(chuàng)建文件dbconfig.php,保存常量<?php define("HOST","localhost"); define("USER","root"); &n
該案例是新聞列表的后臺(tái)管理,涉及數(shù)據(jù)的增刪查改(CURD)操作。
創(chuàng)建一個(gè)新聞列表的數(shù)據(jù)庫(kù):
1. 查詢(xún)數(shù)據(jù)庫(kù)
1.1. 創(chuàng)建文件dbconfig.php,保存常量
<?php define("HOST","localhost");
define("USER","root");
define("PASS","********");
define("DBNAME","news");
1.2. 創(chuàng)建入口文件index.html(連接數(shù)據(jù)庫(kù)、查詢(xún)數(shù)據(jù))
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新聞后臺(tái)管理系統(tǒng)</title>
</head>
<style type="text/css">
.wrapper {width: 1000px;margin: 20px auto;}
h2 {text-align: center;}
.add {margin-bottom: 20px;}
.add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;}
td {text-align: center;}
</style>
<body>
<div>
<h2>新聞后臺(tái)管理系統(tǒng)</h2>
<div>
<a href="addnews.html">增加新聞</a>
</div>
<table width="960" border="1">
<tr>
<th>ID</th>
<th>標(biāo)題</th>
<th>關(guān)鍵字</th>
<th>作者</th>
<th>發(fā)布時(shí)間</th>
<th>內(nèi)容</th>
<th>操作</th>
</tr>
<?php
// 1.導(dǎo)入配置文件
require "dbconfig.php";
// 2. 連接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:數(shù)據(jù)庫(kù)連接失??!");
// 選擇數(shù)據(jù)庫(kù)
mysql_select_db(DBNAME,$link);
// 編碼設(shè)置
mysql_set_charset('utf8',$link);
// 3. 從DBNAME中查詢(xún)到news數(shù)據(jù)庫(kù),返回?cái)?shù)據(jù)庫(kù)結(jié)果集,并按照addtime降序排列
$sql = 'select * from news order by id asc';
// 結(jié)果集
$result = mysql_query($sql,$link);
// var_dump($result);die;
// 解析結(jié)果集,$row為新聞所有數(shù)據(jù),$newsNum為新聞數(shù)目
$newsNum=mysql_num_rows($result);
for($i=0; $i<$newsNum; $i++){
$row = mysql_fetch_assoc($result);
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['keywords']}</td>";
echo "<td>{$row['autor']}</td>";
echo "<td>{$row['addtime']}</td>";
echo "<td>{$row['content']}</td>";
echo "<td>
<a href='javascript:del({$row['id']})'>刪除</a>
<a href='editnews.php?id={$row['id']}'>修改</a>
</td>";
echo "</tr>";
}
// 5. 釋放結(jié)果集
mysql_free_result($result);
mysql_close($link);
?>
</table>
</div>
<script type="text/javascript">
function del (id) {
if (confirm("確定刪除這條新聞嗎?")){
window.location = "action-del.php?id="+id;
}
}
</script>
</body>
</html>
2. 增加新聞
2.1 點(diǎn)擊增加按鈕,通過(guò)頁(yè)面addnews.html添加數(shù)據(jù)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加新聞</title>
</head>
<style type="text/css">
form{
margin: 20px;
}
</style>
<body>
<form action="action-addnews.php" method="post">
<label>標(biāo)題:</label><input type="text" name="title">
<label>關(guān)鍵字:</label><input type="text" name="keywords">
<label>作者:</label><input type="text" name="autor">
<label>發(fā)布時(shí)間:</label><input type="date" name="addtime">
<label>內(nèi)容:</label><input type="text" name="content">
<input type="submit" value="提交">
</form>
</body>
</html>
2.2 創(chuàng)建處理增加新聞的服務(wù)端文件action-addnews.php
<?php
// 處理增加操作的頁(yè)面
require "dbconfig.php";
// 連接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:數(shù)據(jù)庫(kù)連接失??!");
// 選擇數(shù)據(jù)庫(kù)
mysql_select_db(DBNAME,$link);
// 編碼設(shè)置
mysql_set_charset('utf8',$link);
// 獲取增加的新聞
$title = $_POST['title'];
$keywords = $_POST['keywords'];
$autor = $_POST['autor'];
$addtime = $_POST['addtime'];
$content = $_POST['content'];
// 插入數(shù)據(jù)
mysql_query("INSERT INTO news(title,keywords,autor,addtime,content) VALUES ('$title','$keywords','$autor','$addtime','$content')",$link) or die('添加數(shù)據(jù)出錯(cuò):'.mysql_error());
header("Location:demo.php");
3. 刪除新聞
點(diǎn)擊刪除按鈕,通過(guò)服務(wù)端文件action-del.php進(jìn)行刪除處理
<?php
// 處理刪除操作的頁(yè)面
require "dbconfig.php";
// 連接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:數(shù)據(jù)庫(kù)連接失敗!");
// 選擇數(shù)據(jù)庫(kù)
mysql_select_db(DBNAME,$link);
// 編碼設(shè)置
mysql_set_charset('utf8',$link);
$id = $_GET['id'];
//刪除指定數(shù)據(jù)
mysql_query("DELETE FROM news WHERE id={$id}",$link) or die('刪除數(shù)據(jù)出錯(cuò):'.mysql_error());
// 刪除完跳轉(zhuǎn)到新聞頁(yè)
header("Location:demo.php");
4. 修改新聞
4.1 點(diǎn)擊修改按鈕,跳轉(zhuǎn)到文件editnews.php進(jìn)行修改處理
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改新聞</title>
</head>
<body>
<?php
require "dbconfig.php";
$link = @mysql_connect(HOST,USER,PASS) or die("提示:數(shù)據(jù)庫(kù)連接失??!");
mysql_select_db(DBNAME,$link);
mysql_set_charset('utf8',$link);
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM news WHERE id=$id",$link);
$sql_arr = mysql_fetch_assoc($sql);
?>
<form action="action-editnews.php" method="post">
<label>新聞ID: </label><input type="text" name="id" value="<?php echo $sql_arr['id']?>">
<label>標(biāo)題:</label><input type="text" name="title" value="<?php echo $sql_arr['title']?>">
<label>關(guān)鍵字:</label><input type="text" name="keywords" value="<?php echo $sql_arr['keywords']?>">
<label>作者:</label><input type="text" name="autor" value="<?php echo $sql_arr['autor']?>">
<label>發(fā)布時(shí)間:</label><input type="date" name="addtime" value="<?php echo $sql_arr['addtime']?>">
<label>內(nèi)容:</label><input type="text" name="content" value="<?php echo $sql_arr['content']?>">
<input type="submit" value="提交">
</form>
</body>
4.2 通過(guò)服務(wù)端文件action-editnews.php進(jìn)行修改處理
通過(guò)服務(wù)端文件action-editnews.php進(jìn)行修改處理
<?php
// 處理編輯操作的頁(yè)面
require "dbconfig.php";
// 連接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:數(shù)據(jù)庫(kù)連接失??!");
// 選擇數(shù)據(jù)庫(kù)
mysql_select_db(DBNAME,$link);
// 編碼設(shè)置
mysql_set_charset('utf8',$link);
// 獲取修改的新聞
$id = $_POST['id'];
$title = $_POST['title'];
$keywords = $_POST['keywords'];
$autor = $_POST['autor'];
$addtime = $_POST['addtime'];
$content = $_POST['content'];
// 更新數(shù)據(jù)
mysql_query("UPDATE news SET title='$title',keywords='$keywords',autor='$autor',addtime='$addtime',content='$content' WHERE id=$id",$link) or die('修改數(shù)據(jù)出錯(cuò):'.mysql_error());
header("Location:demo.php");
Correcting teacher:西門(mén)大官人Correction time:2019-05-05 10:17:51
Teacher's summary:1、php程序,請(qǐng)使用.php的擴(kuò)展名
2、不要在函數(shù)前加@,這會(huì)屏蔽錯(cuò)誤,不利于開(kāi)發(fā)階段的錯(cuò)誤排查