PS:纠结在各种表单验证上了。不过收获还是很大!暂时先不把项目布局列出来了,等做完一起整理吧!
#首先创建子版块数据表
mysql> show create table sfk_son_module\G
*************************** 1. row ***************************
Table: sfk_son_module
Create Table: CREATE TABLE `sfk_son_module` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`father_module_id` int(10) unsigned NOT NULL,
`module_name` varchar(32) NOT NULL,
`info` text,
`member_id` int(10) unsigned NOT NULL,
`sort` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
#表结构如下
mysql> describe sfk_son_module;
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| father_module_id | int(10) unsigned | NO | | NULL | |
| module_name | varchar(32) | NO | | NULL | |
| info | text | YES | | NULL | |
| member_id | int(10) unsigned | NO | | NULL | |
| sort | int(11) | YES | | 0 | |
+------------------+------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
ps:之前我文件命名的方式有些随意了~后面尽量改正
子版块添加页 son_module_add.php:
<?php
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
include_once '../inc/tool.inc.php';
$title = '子板块添加';//设置当前页面标题
$link = sql_connect();//数据库连接
if(isset($_POST['submit'])){
//一、表单验证
//1):如果没有选择父板块
if($_POST['father_module_id'] == 0){
skip('son_module_add.php','error','请选择一个父板块');
}
//2):如果没有填写板块名称
//这里有一个小bug,就是不能输入为0板块
//用isset就检测不出来
//用empty 0板块就不通过
if(empty($_POST['module_name'])){
skip('son_module_add.php','error','请填写子板块名称');
}
//3):如果板块名称超过32个字符:
if(mb_strlen($_POST['module_name'],'utf8') > 32){
skip('son_module_add.php','error','子版块名称超过32个字符!');
}
//4):如果板块简介超过255个字符:
if(mb_strlen($_POST['info'],'utf8') > 255){
skip('son_module_add.php','error','子版块简介信息长度超过255个字符!');
}
//5):如果排序不是一个数字:
if(!is_numeric($_POST['sort'])){
skip('son_module_add.php','error','排序只能是一个数字!');
}
//二、入库前的验证
//1):如果非法选择父板块,或父板块id不存在
$query = "select * from sfk_father_module
where id = {$_POST['father_module_id']}";
$result = sql_execute($link,$query);
if(mysqli_num_rows($result) < 1){
skip('son_module_add.php','error','所选父板块不存在!');
}
//2):如果子板块已经存在:
$query = "select * from sfk_son_module
where module_name = '{$_POST['module_name']}'";
$result = sql_execute($link,$query);
if(mysqli_num_rows($result) > 0){
skip('son_module_add.php','error','子版块已经存在!');
}
//3):入库前将数据转义
$_POST = sql_escape($link,$_POST);
//三、入库
$query = "insert into sfk_son_module(father_module_id,module_name,info,member_id,sort)
values({$_POST['father_module_id']},
'{$_POST['module_name']}',
'{$_POST['info']}',
{$_POST['member_id']},
{$_POST['sort']})";
sql_execute($link,$query);
//四、入库后的验证
if(mysqli_affected_rows($link) == 1){
skip('son_module_add.php','ok','添加子版块成功!');
}else{
skip('son_module_add.php','error','添加失败!');
}
}
?>
<?php include_once './inc/header.inc.php';?>
<div id="main" style="height:1000px;">
<div class="title" style='margin-bottom:20px;'>子板块添加</div>
<form method="POST">
<table class="au">
<tr>
<td>所属父板块</td>
<td>
<select name="father_module_id">
<option value="0">===请选择父板块===</option>
<?php
$query = "select * from sfk_father_module";
$result = sql_execute($link,$query);
while($data = mysqli_fetch_assoc($result)){
echo "<option value = '{$data['id']}'>{$data['module_name']}</option>";
}
?>
</select>
</td>
<td>必选项</td>
</tr>
<tr>
<td>版块名称</td>
<td><input type="text" name = 'module_name' /></td>
<td>版块名称不得为空,最大不得超过66个字符</td>
</tr>
<tr>
<td>板块简介</td>
<td><textarea name="info" cols="30" rows="10"></textarea></td>
<td>简介不得多于255个字符</td>
</tr>
<tr>
<td>版主</td>
<td>
<select name="member_id">
<option value="0">===请选择版主===</option>
</select>
</td>
<td>可选项</td>
</tr>
<tr>
<td>排序</td>
<td><input type="text" name = 'sort' value = '0' /></td>
<td>填写一个数字即可</td>
</tr>
</table>
<input class="btn" type="submit" name="submit" value="添加" style='margin-top: 10px;'/>
</form>
</div>
<?php include_once './inc/footer.inc.php'; ?>
由于还没有做子版块列表的展示,所以通过mysql查询一下数据:
修改子版块son_module_update.php:
<?php
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
include_once '../inc/tool.inc.php';
$title = '子板块添加';//设置当前页面标题
//$_GET传值验证:
//如果id不存在或者存在注入行为:
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
skip('son_module.php','error','id参数填写错误!');
}
//获取当前修改页面的默认值
$link = sql_connect();
$query = "select * from sfk_son_module where id = {$_GET['id']}";
$res = sql_execute($link,$query);
//如果获取到有结果集
if(mysqli_num_rows($res) == 1){
//取到当前页面的默认值
$default = mysqli_fetch_assoc($res);
}
//表单验证--是否点击修改按钮
if(isset($_POST['submit'])){
//表单验证+入库前验证+转义字符:
$check_flag = 'update';
include_once './inc/check_son_module.inc.php';
//入库
$query = "update sfk_son_module
set father_module_id = {$_POST['father_module_id']},
module_name = '{$_POST['module_name']}',
info = '{$_POST['info']}',
member_id = {$_POST['member_id']},
sort = {$_POST['sort']}
where id = {$_GET['id']}
";
sql_execute($link,$query);
//入库后的验证:
//1)如果用户不做任何操作直接提交:
//0行受到影响
//意味着受影响行数为0,板块名称,简介,排序,版主,父板块都为默认值:
if(mysqli_affected_rows($link) == 0){
$query = "select * from sfk_son_module where id = {$_GET['id']}";
$res = sql_execute($link,$query);
$default_flag = mysqli_fetch_assoc($res);
if($default == $default_flag){
skip('son_module.php','ok','您没有做任何修改!');
}else{
skip('son_module.php','ok','修改失败!');
}
}
//2)如果1行受到影响
if(mysqli_affected_rows($link) == 1){
skip('son_module.php','ok','修改成功!');
}
}
?>
<?php include_once './inc/header.inc.php';?>
<div id="main" style="height:1000px;">
<div class="title" style='margin-bottom:20px;'>子板块添加</div>
<form method="POST">
<table class="au">
<tr>
<td>所属父板块</td>
<td>
<select name="father_module_id">
<option value="0">===请选择父板块===</option>
<?php
$query = "select * from sfk_father_module";
$result = sql_execute($link,$query);
while($data = mysqli_fetch_assoc($result)){
$str = " ";
if($default['father_module_id'] == $data['id']){
$str = "selected = 'selected'";
}
echo "<option value = '{$data['id']}'{$str}>{$data['module_name']}</option>";
}
?>
</select>
</td>
<td>必选项</td>
</tr>
<tr>
<td>版块名称</td>
<td><input type="text" name = 'module_name' value = "<?php echo $default['module_name']?>" /></td>
<td>版块名称不得为空,最大不得超过66个字符</td>
</tr>
<tr>
<td>板块简介</td>
<td><textarea name="info" cols="30" rows="10"><?php echo $default['info']?></textarea></td>
<td>简介不得多于255个字符</td>
</tr>
<tr>
<td>版主</td>
<td>
<select name="member_id">
<option value="0">===请选择版主===</option>
</select>
</td>
<td>可选项</td>
</tr>
<tr>
<td>排序</td>
<td><input type="text" name = 'sort' value = "<?php echo $default['sort']?>" /></td>
<td>填写一个数字即可</td>
</tr>
</table>
<input class="btn" type="submit" name="submit" value="修改" style='margin-top: 10px;'/>
</form>
</div>
<?php include_once './inc/footer.inc.php'; ?>