敲-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 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">&nbsp;<?php echo $value['id']?></td>
<td bgcolor="#FFFFFF">&nbsp;<?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 style="color:#ccc;font-size:14px;">  作者:<!--作者放置到这里--><?php echo $value['author']?></span></h1>
<div class="entry">
<?php echo $value['description']?>
</div>
<div class="meta">
<p class="links"><a href="article.show.php?id=<?php echo $value['id']?>" class="more">查看详细</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;</p>
</div>
</div>
<?php
}
}
?>
</div>
<!-- end content -->
<!-- start sidebar -->
<div id="sidebar">
<ul>
<li id="search">
<h2><b class="text1">Search</b></h2>
<form method="get" action="article.search.php">
<fieldset>
<input type="text" id="s" name="key" value="" />
<input type="submit" id="x" value="Search" />
</fieldset>
</form>
</li>
</ul>
</div>
<!-- end sidebar -->
<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p id="legal"></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 id=$id";
$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 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="#">文章</a></li>
<li><a href="#">关于我们</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</div>
</div>
<!-- end header -->
</div>

<!-- start page -->
<div id="page">
<!-- start content -->
<div id="content">
<div class="post">
<h1 class="title"><!--文章标题放置到这里--><?php echo $row['title']?><span style="color:#ccc;font-size:14px;">  作者:<!--作者放置到这里--><?php echo $row['author'];?></span></h1>
<div class="entry">
<!--文章内容放置到这里-->
<?php echo $row['content']?>
</div>
</div>
</div>
<!-- end content -->
<!-- start sidebar -->
<div id="sidebar">
<ul>
<li id="search">
<h2><b class="text1">Search</b></h2>
<form method="get" action="">
<fieldset>
<input type="text" id="s" name="s" value="" />
<input type="submit" id="x" value="Search" />
</fieldset>
</form>
</li>
</ul>
</div>
<!-- end sidebar -->
<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p id="legal"></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 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 style="color:#ccc;font-size:14px;">  作者:<!--作者放置到这里--><?php echo $value['author']?></span></h1>
<div class="entry">
<?php echo $value['description']?>
</div>
<div class="meta">
<p class="links"><a href="article.show.php?id=<?php echo $value['id']?>" class="more">查看详细</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;</p>
</div>
</div>
<?php
}
}
?>
</div>
<!-- end content -->
<!-- start sidebar -->
<div id="sidebar">
<ul>
<li id="search">
<h2><b class="text1">Search</b></h2>
<form method="get" action="">
<fieldset>
<input type="text" id="s" name="s" value="" />
<input type="submit" id="x" value="Search" />
</fieldset>
</form>
</li>
</ul>
</div>
<!-- end sidebar -->
<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p id="legal"></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 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">

<div class="post">
<h1 class="title">关于我们</h1>
<div class="entry">
<?php echo $about?>
</div>

</div>

</div>
<!-- end content -->
<!-- start sidebar -->
<div id="sidebar">
<ul>
<li id="search">
<h2><b class="text1">Search</b></h2>
<form method="get" action="">
<fieldset>
<input type="text" id="s" name="s" value="" />
<input type="submit" id="x" value="Search" />
</fieldset>
</form>
</li>
</ul>
</div>
<!-- end sidebar -->
<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p id="legal"></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 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">

<div class="post">
<h1 class="title">联系我们</h1>
<div class="entry">
<?php echo $contact?>
</div>

</div>

</div>
<!-- end content -->
<!-- start sidebar -->
<div id="sidebar">
<ul>
<li id="search">
<h2><b class="text1">Search</b></h2>
<form method="get" action="">
<fieldset>
<input type="text" id="s" name="s" value="" />
<input type="submit" id="x" value="Search" />
</fieldset>
</form>
</li>
</ul>
</div>
<!-- end sidebar -->
<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">

</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);

结果

Hello!

 

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实例讲解

 

转载于:https://www.cnblogs.com/andy1202go/p/5015568.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值