敲-PHP与MySQL,JSON
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 );
$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" 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 ><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 ;
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 ;
aritcle.del.handle.php
<?php
require_once('../connect.php');
//读取id号。不像其他的是有传递值的
$id = $_GET['id'];
$deletesql = "delete from article where ;
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" 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" 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']?>">删除</a> <a href="article.modify.php?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 >联系我们</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>
<!-- start page -->
<div ></p>
</div>
<!-- end footer -->
</body>
</html>
6.2 文章详情页
article.show.php
<?php
require_once('connect.php');
$id = intval($_GET['id']);
$sql = "select * from article where ;
$query = mysqli_query($con,$sql);
if($query&&mysqli_num_rows($query)){
$row = mysqli_fetch_assoc($query);
}else{
echo "这篇文章不存在";
exit;
}
?>
<!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 >联系我们</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>
<!-- start page -->
<div ></p>
</div>
<!-- end footer -->
</body>
</html>
6.3 检索
article.search.php
这里的查询的是根据题目来查询。
<?php
require_once('connect.php');
$key = $_GET['key'];
$sql = "select * from article where title like '%$key%' 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 >联系我们</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>
<!-- start page -->
<div ></p>
</div>
<!-- end footer -->
</body>
</html>
6.4 关于我们和联系我们
about.php
<?php
require_once('connect.php');
$sql = "select * from introduce";
$query = mysqli_query($con,$sql);
//if($query&&mysqli_num_rows($query)){
$about = "啦啦啦";
//}
?>
<!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 >联系我们</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>
<!-- start page -->
<div ></p>
</div>
<!-- end footer -->
</body>
</html>
contact.php
<?php
require_once('connect.php');
$sql = "select * from introduce";
$query = mysqli_query($con,$sql);
//$arr=mysqli_fetch_array($query, MYSQL_ASSOC);
//if($query&&mysqli_num_rows($query)){
$contact = "asdfadf";
//}
?>
<!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 >联系我们</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>
<!-- start page -->
<div >
</div>
<!-- end footer -->
</body>
</html>
------------------------------------------
2、JSON基础
1.2 JSON的使用
--JSON与serialize数据格式的异同和使用
相同点:把其他数据类型转换为一个可以传输的字符串;都是结构性数据;
不同点:s序列化之后,保存数据原有类型——适用于存储带有加密方式的数据串;JSON更简洁——适合数据量大,不要求数据类型保存的情况
举个栗子
<?php
//输出模块
function createHtmlTag($tag=""){
echo "<h1>$tag</h1><br/>";
}
createHtmlTag("Hello!");
createHtmlTag("JSON和serialize的对比");
//测试用数组
$member=array("username","age");
var_dump($member);
$jsonObj=json_encode($member);
$serializeObj=serialize($member);
createHtmlTag($jsonObj);
createHtmlTag($serializeObj);
结果
JSON和serialize的对比
array(size=2) 0 =>
string
'username' (length=8) 1 =>
string
'age' (length=3)
["username","age"]
a:2:{i:0;s:8:"username";i:1;s:3:"age";}
--常用JSON函数
json_encode()——JSON加密
json_decode()——解密
1.3 JSON实例讲解