根据数据库中的三张表格:timu,xuanxiang,kemu来进行数据的增删改查,并且使用smarty模版将前端与后台分离开来
三张表格:
主页面后台 main.php:
<?php
//引入配置文件
include("../init.inc.php");
//引入数据库
include ("../../DBDA.class.php");
$db=new DBDA();
$sql="select * from timu";
$attr=$db->Query($sql);
//注册变量
$smarty->assign("shuju",$attr);
$smarty->display("main.html");
主页面前端 main.html:
<!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>
</head> <body>
<h1>主页面</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>题目名称</td>
<td>答案</td>
<td>所属科目</td>
<td>难度</td>
<td>类型</td>
<td>操作</td>
</tr> <{foreach $shuju as $v}>
<tr>
<td><{$v[1]}></td>
<td><{$v[2]}></td>
<td><{$v[3]}></td>
<td><{$v[4]}></td>
<td><{$v[5]}></td>
<td><a href="delete.php?code=<{$v[0]}>">删除</a>
<a href="update.php?code=<{$v[0]}>">修改</a>
</td>
</tr>
<{/foreach}>
</table><br /> <a href="add.php">添加数据</a> </body>
</html>
运行主页面:
添加数据后台页面 add.php:
<?php
include("../init.inc.php"); include("../../DBDA.class.php");
$db=new DBDA();
$sql="select * from kemu";
$attr=$db->Query($sql);
$smarty->assign("kemu",$attr); $smarty->display("add.html");
添加数据前端页面 add.html:
<!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>
</head> <body>
<h1>添加数据</h1> <form action="addchuli.php" method="post">
<div>请输入题目名称:<input type="text" name="name" /></div>
<div>请输入选项A:<input type="text" name="a" /></div>
<div>请输入选项B:<input type="text" name="b" /></div>
<div>请输入选项C:<input type="text" name="c" /></div>
<div>请输入选项D:<input type="text" name="d" /></div> <div>请输入题目答案:<input type="text" name="daan" /></div>
<div>请输入题目科目:
<select name="kemu">
<{foreach $kemu as $v}>
<option value="<{$v[0]}>"><{$v[1]}></option>
<{/foreach}>
</select>
</div>
<div>请输入题目难度:
<select name="nandu">
<option value="0">简单</option>
<option value="1">适中</option>
<option value="2">困难</option>
</select>
</div>
<div>请输入题目类型:
<select name="type">
<option value="0">判断</option>
<option value="1">单选</option>
<option value="2">多选</option>
</select>
</div>
<input type="submit" value="添加" />
<a href="main.php">返回主页面</a>
</form> </body>
</html>
添加数据处理页面addchuli.php:
<?php include ("../../DBDA.class.php");
$db=new DBDA();
$name=$_POST["name"];
$daan=$_POST["daan"];
$kemu=$_POST["kemu"];
$nandu=$_POST["nandu"];
$type=$_POST["type"]; $a=$_POST["a"];
$b=$_POST["b"];
$c=$_POST["c"];
$d=$_POST["d"]; //添加题目
$sql="insert into timu values ('','{$name}','{$daan}','{$kemu}','{$nandu}','{$type}')";
echo $sql;
if($db->Query($sql,1))
{
//添加选项
$id=$db->conn->insert_id;
$sqla="insert into xuanxiang values('','{$a}','A','{$id}')";
$db->Query($sqla,1);
$sqlb="insert into xuanxiang values('','{$b}','B','{$id}')";
$db->Query($sqlb,1);
$sqlc="insert into xuanxiang values('','{$c}','C','{$id}')";
$db->Query($sqlc,1);
$sqld="insert into xuanxiang values('','{$d}','D','{$id}')";
$db->Query($sqld,1);
header("location:add.php");
}
else
{
echo "添加失败!";
}
运行添加页面:
删除数据处理页面 delete.php:
<?php
$code=$_GET["code"];
include("../../DBDA.class.php");
$db=new DBDA();
//删除选项表中的题目代号
$sql="delete from xuanxiang where timu='{$code}'";
$db->Query($sql,1); //删除题目表中的代号
$sql1="delete from timu where code='{$code}'";
$db->Query($sql1,1); header("location:main.php");
修改数据后台页面 update.php:
<?php
include("../init.inc.php");
//引入数据库
include ("../../DBDA.class.php");
$db=new DBDA();
$code=$_GET["code"];
//向前端传入题目的值
$sql="select * from timu where code='{$code}'";
$atimu=$db->Query($sql);
//向前端传入选项的值
$sql="select * from xuanxiang where timu='{$code}'";
$axuan=$db->Query($sql);
//向前端传入科目的值
$sql="select * from kemu";
$akemu=$db->Query($sql); $smarty->assign("timu",$atimu);
$smarty->assign("xuanxiang",$axuan);
$smarty->assign("kemu",$akemu); $smarty->display("update.html");
修改数据前端页面 update.html:
<!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>
</head> <body>
<h1>修改数据</h1> <form action="updatechuli.php" method="post">
<input type="hidden" name="code" value="<{$timu[0][0]}>">
<div>请输入题目名称:<input type="text" name="name" value="<{$timu[0][1]}>" /></div> <{foreach $xuanxiang as $v}>
<div>请输入选项<{$v[2]}>:<input type="text" name="<{$v[2]}>" value="<{$v[1]}>" /></div>
<{/foreach}> <div>请输入题目答案:<input type="text" name="daan" value="<{$timu[0][2]}>"/></div> <div>请输入题目科目:
<select name="kemu">
<{foreach $kemu as $v}>
<!-- 判断哪条选中了 -->
<{if $v[0]==$timu[0][3]}>
<option selected="selected" value="<{$v[0]}>"><{$v[1]}></option>
<{else}>
<option value="<{$v[0]}>"><{$v[1]}></option> <{/if}> <{/foreach}>
</select>
</div>
<div>请输入题目难度:
<select name="nandu">
<{if $timu[0][4]==0}>
<option selected="selected" value="0">简单</option>
<{else}>
<option value="0">简单</option>
<{/if}> <{if $timu[0][4]==1}>
<option selected="selected" value="1">适中</option>
<{else}>
<option value="1">适中</option>
<{/if}> <{if $timu[0][4]==2}>
<option selected="selected" value="2">困难</option>
<{else}>
<option value="2">困难</option>
<{/if}> </select>
</div>
<div>请输入题目类型:
<select name="type">
<{if $timu[0][4]==0}>
<option selected="selected" value="0">判断</option>
<{else}>
<option value="0">判断</option>
<{/if}> <{if $timu[0][4]==1}>
<option selected="selected" value="1">单选</option>
<{else}>
<option value="1">单选</option>
<{/if}> <{if $timu[0][4]==2}>
<option selected="selected" value="2">多选</option>
<{else}>
<option value="2">多选</option>
<{/if}> </select>
</div>
<input type="submit" value="修改" />
<a href="main.php">返回主页面</a>
</form> </body>
</html>
修改数据处理页面 updatechulu.php:
<?php include ("../../DBDA.class.php");
$db=new DBDA();
$code=$_POST["code"]; $name=$_POST["name"];
$daan=$_POST["daan"];
$kemu=$_POST["kemu"];
$nandu=$_POST["nandu"];
$type=$_POST["type"]; //修改题目
$sql1="update timu set name='{$name}',daan='{$daan}',kemu='{$kemu}',nandu='{$nandu}',type='{$type}' where code='{$code}'"; if($db->Query($sql1,1))
{
//删除选项
$sdxx = "delete from xuanxiang where timu='{$code}'";
$db->Query($sdxx,1); if(!empty($_POST["A"]))
{
$a=$_POST["A"];
$sqla="insert into xuanxiang values('','{$a}','A','{$code}')"; $db->Query($sqla,1);
} if(!empty($_POST["B"]))
{
$b=$_POST["B"];
$sqlb="insert into xuanxiang values('','{$b}','B','{$code}')";
$db->Query($sqlb,1);
} if(!empty($_POST["C"]))
{
$c=$_POST["C"];
$sqlc="insert into xuanxiang values('','{$c}','C','{$code}')";
$db->Query($sqlc,1);
} if(!empty($_POST["D"]))
{
$d=$_POST["D"];
$sqld="insert into xuanxiang values('','{$d}','D','{$code}')";
$db->Query($sqld,1);
} header("location:main.php");
} else
{
echo "修改失败!";
}