hi
敲代碼~
1、php與mysql
5.4 修改界面
同樣是界面和程序。
界面article.modify.php
<?php
require_once('../connect.php');
//讀取舊信息
$id = $_GET['id'];
$query = mysqli_query($con,"select * from article where id=$id");
$data = mysqli_fetch_assoc($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
</style>
</head>
<body>
<table width="100%" height="520" border="0" cellpadding="8" cellspacing="1" bgcolor="#000000">
<tr>
<td height="89" colspan="2" bgcolor="#FFFF99"><strong>後台管理系統</strong></td>
</tr>
<tr>
<td width="213" height="287" align="left" valign="top" bgcolor="#FFFF99"><p><a href="article.add.php">發布文章</a></p>
<p><a href="article.manage.php">管理文章</a></p> <a href="article.add.php"></a></td>
<td width="854" valign="top" bgcolor="#FFFFFF"><form id="form1" name="form1" method="post" action="article.modify.handle.php">
<input type="hidden" name="id" value="<?php echo $data['id']?>" />
<table width="590" border="0" cellpadding="8" cellspacing="1">
<tr>
<td colspan="2" align="center">修改文章</td>
</tr>
<tr>
<td width="119">標題</td>
<td width="437"><label for="title"></label>
<input type="text" name="title" id="title" value="<?php echo $data['title']?>"/></td>
</tr>
<tr>
<td>作者</td>
<td><input type="text" name="author" id="author" value="<?php echo $data['author']?>"/></td>
</tr>
<tr>
<td>簡介</td>
<td><label for="description"></label>
<textarea name="description" id="description" cols="60" rows="5"><?php echo $data['description']?></textarea></td>
</tr>
<tr>
<td>內容</td>
<td><textarea name="content" cols="60" rows="20" id="content"><?php echo $data['content']?></textarea></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="button" id="button" value="提交" /></td>
</tr>
</table>
</form></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFF99"><strong>版權所有</strong></td>
</tr>
</table>
</body>
</html>
關鍵點在於php的讀取,以及在html中value的php調用。
修改程序article.modify.handle.php
<?php
//和數據庫有關的,無條件寫上這麼一句話
require_once('../connect.php');
//接受修改後的數據(表單傳遞)
$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$description = $_POST['description'];
$content = $_POST['content'];
$dateline = time();
//寫sql修改語句,並做成功與否的判斷,並跳回修改界面
$updatesql = "update article set title='$title',author='$author',description='$description',content='$content',dateline=$dateline where id=$id";
if(mysqli_query($con,$updatesql)){
echo "<script>alert('修改文章成功');window.location.href='article.manage.php';</script>";
}else{
echo "<script>alert('修改文章失敗');window.location.href='article.manage.php';</script>";
}
?>
5.5 文章刪除
先做需求的分析:同上面幾個略有區別,刪除文章不需要界面,只需要一個刪除按鈕來掉要就行了。所以只有一個文件。而關鍵的sql語句只有一句
$delsql="delete from article where id=$id";
aritcle.del.handle.php
<?php
require_once('../connect.php');
//讀取id號。不像其他的是有傳遞值的
$id = $_GET['id'];
$deletesql = "delete from article where id=$id";
if(mysql_query($deletesql)){
echo "<script>alert('刪除文章成功');window.location.href='article.manage.php';</script>";
}else{
echo "<script>alert('刪除文章失敗');window.location.href='article.manage.php';</script>";
}
?>
5.6 文章管理列表
需求分析:列表顯示出所有的文章,然後後面有兩個按鈕,刪除(鏈接至上一節的刪除模塊)和修改(鏈接至之前的模塊)
所以,只需要一個文件,顯示模塊就好
article.manage.php
<?php
require_once('../connect.php');
$sql = "select * from article order by dateline desc";
$query = mysqli_query($con,$sql);
if($query&&mysqli_num_rows($query)){
while($row =mysqli_fetch_assoc($query)){
$data[] = $row;
}
}else{
$data = array();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
</style>
</head>
<body>
<table width="100%" height="520" border="0" cellpadding="8" cellspacing="1" bgcolor="#000000">
<tr>
<td height="89" colspan="2" bgcolor="#FFFF99"><strong>後台管理系統</strong></td>
</tr>
<tr>
<td width="156" height="287" align="left" valign="top" bgcolor="#FFFF99"><p><a href="article.add.php">發布文章</a></p>
<p><a href="article.manage.php">管理文章</a></p></td>
<td width="837" valign="top" bgcolor="#FFFFFF"><table width="743" border="0" cellpadding="8" cellspacing="1" bgcolor="#000000">
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF">文章管理列表</td>
</tr>
<tr>
<td width="37" bgcolor="#FFFFFF">編號</td>
<td width="572" bgcolor="#FFFFFF">標題</td>
<td width="82" bgcolor="#FFFFFF">操作</td>
</tr>
<?php
if(!empty($data)){
foreach($data as $value){
?>
<tr>
<td bgcolor="#FFFFFF"> <?php echo $value['id']?></td>
<td bgcolor="#FFFFFF"> <?php echo $value['title']?></td>
<td bgcolor="#FFFFFF"><a href="article.del.handle.php?id=<?php echo $value['id']?>">刪除</a> <a href="article.modify.php?id=<?php echo $value['id']?>">修改</a></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFF99"><strong>版權所有</strong></td>
</tr>
</table>
</body>
</html>
5.7 函數總結
mysqli_connect()
mysqli_select_db()
mysqli_query()
mysqli_error()
mysqli_fetch_assoc()
mysqli_num_rows()
六、前台管理界面的開發
6.1 文章列表
article.list.php
<?php
require_once('connect.php');
$sql = "select * from article order by dateline desc";
$query = mysqli_query($con,$sql);
if($query&&mysqli_num_rows($query)){
while($row = mysqli_fetch_assoc($query)){
$data[] = $row;
}
}
?>
<!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">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>文章發布系統</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<!-- start header -->
<div id="header">
<div id="logo">
<h1><a href="#">php與mysql<sup></sup></a></h1>
<h2></h2>
</div>
<div id="menu">
<ul>
<li class="active"><a href="article.list.php">文章</a></li>
<li><a href="about.php">關於我們</a></li>
<li><a href="contact.php">聯系我們</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>
<!-- start page -->
<div id="page">
<!-- start content -->
<div id="content">
<?php
if(empty($data)){
echo "當前沒有文章,請管理員在後台添加文章";
}else{
foreach($data as $value){
?>
<div class="post">
<h1 class="title"><?php echo $value['title']?><span>array (size=2)
0 =>
string
'username' (length=8) 1 =>
string
'age' (length=3)
--常用JSON函數
json_encode()——JSON加密
json_decode()——解密
1.3 JSON實例講解