smarty练习:数据的增删改

根据数据库中的三张表格:timu,xuanxiang,kemu来进行数据的增删改查,并且使用smarty模版将前端与后台分离开来 

三张表格:

 

主页面后台 main.php:

 1 <?php
 2 //引入配置文件
 3 include("../init.inc.php");
 4 //引入数据库
 5 include ("../../DBDA.class.php");
 6 $db=new DBDA();
 7 $sql="select * from timu";
 8 $attr=$db->Query($sql);
 9 //注册变量
10 $smarty->assign("shuju",$attr);
11 $smarty->display("main.html");
View Code

 

主页面前端 main.html:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>主页</title>
 6 </head>
 7 
 8 <body>
 9 <h1>主页面</h1>
10 <table width="100%" border="1" cellpadding="0" cellspacing="0">
11 <tr>
12     <td>题目名称</td>
13     <td>答案</td>
14     <td>所属科目</td>
15     <td>难度</td>
16     <td>类型</td>
17     <td>操作</td>
18 </tr>
19 
20 <{foreach $shuju as $v}>
21 <tr>
22     <td><{$v[1]}></td>
23     <td><{$v[2]}></td>
24     <td><{$v[3]}></td>
25     <td><{$v[4]}></td>
26     <td><{$v[5]}></td>
27     <td><a href="delete.php?code=<{$v[0]}>">删除</a>
28     <a href="update.php?code=<{$v[0]}>">修改</a>
29     </td>
30 </tr>
31 <{/foreach}>
32 </table><br />
33 
34 <a href="add.php">添加数据</a>
35 
36 </body>
37 </html>
View Code

 运行主页面:

 

 

添加数据后台页面 add.php:

 1 <?php
 2 include("../init.inc.php");
 3 
 4 include("../../DBDA.class.php");
 5 $db=new DBDA();
 6 $sql="select * from kemu";
 7 $attr=$db->Query($sql);
 8 $smarty->assign("kemu",$attr);
 9 
10 $smarty->display("add.html");
View Code

 

添加数据前端页面 add.html:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>添加数据</title>
 6 </head>
 7 
 8 <body>
 9 <h1>添加数据</h1>
10 
11 <form action="addchuli.php" method="post">
12 <div>请输入题目名称:<input type="text" name="name" /></div>
13 <div>请输入选项A:<input type="text" name="a" /></div>
14 <div>请输入选项B:<input type="text" name="b" /></div>
15 <div>请输入选项C:<input type="text" name="c" /></div>
16 <div>请输入选项D:<input type="text" name="d" /></div>
17 
18 <div>请输入题目答案:<input type="text" name="daan" /></div>
19 <div>请输入题目科目:
20     <select name="kemu">
21     <{foreach $kemu as $v}>
22         <option value="<{$v[0]}>"><{$v[1]}></option>
23     <{/foreach}>
24     </select>
25 </div>
26 <div>请输入题目难度:
27     <select name="nandu">
28         <option value="0">简单</option>
29         <option value="1">适中</option>
30         <option value="2">困难</option>
31     </select>
32 </div>
33 <div>请输入题目类型:
34     <select name="type">
35         <option value="0">判断</option>
36         <option value="1">单选</option>
37         <option value="2">多选</option>
38     </select>
39 </div>
40 <input type="submit" value="添加" />
41 <a href="main.php">返回主页面</a>
42 </form>
43 
44 </body>
45 </html>
View Code

 

添加数据处理页面addchuli.php:

 1 <?php
 2 
 3 include ("../../DBDA.class.php");
 4 $db=new DBDA();
 5 $name=$_POST["name"];
 6 $daan=$_POST["daan"];
 7 $kemu=$_POST["kemu"];
 8 $nandu=$_POST["nandu"];
 9 $type=$_POST["type"];
10 
11 $a=$_POST["a"];
12 $b=$_POST["b"];
13 $c=$_POST["c"];
14 $d=$_POST["d"];
15 
16 //添加题目
17 $sql="insert into timu values ('','{$name}','{$daan}','{$kemu}','{$nandu}','{$type}')";
18 echo $sql;
19 if($db->Query($sql,1))
20 {
21     //添加选项
22     $id=$db->conn->insert_id;
23     $sqla="insert into xuanxiang values('','{$a}','A','{$id}')";
24     $db->Query($sqla,1);
25     $sqlb="insert into xuanxiang values('','{$b}','B','{$id}')";
26     $db->Query($sqlb,1);
27     $sqlc="insert into xuanxiang values('','{$c}','C','{$id}')";
28     $db->Query($sqlc,1);
29     $sqld="insert into xuanxiang values('','{$d}','D','{$id}')";
30     $db->Query($sqld,1);
31     header("location:add.php");
32     }
33 else
34 {
35     echo "添加失败!";
36     }
View Code

 运行添加页面:

 

 

删除数据处理页面 delete.php:

 1 <?php
 2 $code=$_GET["code"];
 3 include("../../DBDA.class.php");
 4 $db=new DBDA();
 5 //删除选项表中的题目代号
 6 $sql="delete from xuanxiang where timu='{$code}'";
 7 $db->Query($sql,1);
 8 
 9 //删除题目表中的代号
10 $sql1="delete from timu where code='{$code}'";
11 $db->Query($sql1,1);
12 
13 header("location:main.php");
View Code

 

 

修改数据后台页面 update.php:

 1 <?php
 2 include("../init.inc.php");
 3 //引入数据库
 4 include ("../../DBDA.class.php");
 5 $db=new DBDA();
 6 $code=$_GET["code"];
 7 //向前端传入题目的值
 8 $sql="select * from timu where code='{$code}'";
 9 $atimu=$db->Query($sql);
10 //向前端传入选项的值
11 $sql="select * from xuanxiang where timu='{$code}'";
12 $axuan=$db->Query($sql);
13 //向前端传入科目的值
14 $sql="select * from kemu";
15 $akemu=$db->Query($sql);
16 
17 $smarty->assign("timu",$atimu);
18 $smarty->assign("xuanxiang",$axuan);
19 $smarty->assign("kemu",$akemu);
20 
21 $smarty->display("update.html");
View Code

 

修改数据前端页面 update.html:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>修改数据</title>
 6 </head>
 7 
 8 <body>
 9 <h1>修改数据</h1>
10 
11 <form action="updatechuli.php" method="post">
12 <input type="hidden" name="code" value="<{$timu[0][0]}>"> 
13 <div>请输入题目名称:<input type="text" name="name" value="<{$timu[0][1]}>" /></div>
14 
15 <{foreach $xuanxiang as $v}>
16 <div>请输入选项<{$v[2]}>:<input type="text" name="<{$v[2]}>" value="<{$v[1]}>" /></div>
17 <{/foreach}>
18 
19 <div>请输入题目答案:<input type="text" name="daan"  value="<{$timu[0][2]}>"/></div>
20 
21 
22 <div>请输入题目科目:
23     <select name="kemu">
24     <{foreach $kemu as $v}>
25    <!-- 判断哪条选中了 -->
26         <{if $v[0]==$timu[0][3]}>
27         <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option>
28         <{else}>
29           <option value="<{$v[0]}>"><{$v[1]}></option>
30         
31         <{/if}>        
32         
33     <{/foreach}>
34     </select>
35 </div>
36 <div>请输入题目难度:
37     <select name="nandu">
38         <{if $timu[0][4]==0}>
39           <option selected="selected" value="0">简单</option>
40          <{else}>
41            <option value="0">简单</option>
42         <{/if}>
43         
44         <{if $timu[0][4]==1}>
45           <option selected="selected" value="1">适中</option>
46          <{else}>
47            <option value="1">适中</option>
48         <{/if}>
49         
50         <{if $timu[0][4]==2}>
51           <option selected="selected" value="2">困难</option>
52          <{else}>
53            <option value="2">困难</option>
54         <{/if}>
55        
56     </select>
57 </div>
58 <div>请输入题目类型:
59     <select name="type">
60         <{if $timu[0][4]==0}>
61          <option selected="selected" value="0">判断</option>
62          <{else}>
63           <option value="0">判断</option>
64         <{/if}>
65         
66         <{if $timu[0][4]==1}>
67         <option selected="selected" value="1">单选</option>
68          <{else}>
69          <option value="1">单选</option>
70         <{/if}>
71         
72          <{if $timu[0][4]==2}>
73         <option selected="selected" value="2">多选</option>
74          <{else}>
75          <option value="2">多选</option>
76         <{/if}>
77  
78     </select>
79 </div>
80 <input type="submit" value="修改" />
81 <a href="main.php">返回主页面</a>
82 </form>
83 
84 </body>
85 </html>
View Code

 

修改数据处理页面 updatechulu.php:

 1 <?php
 2 
 3 include ("../../DBDA.class.php");
 4 $db=new DBDA();
 5 $code=$_POST["code"];
 6 
 7 $name=$_POST["name"];
 8 $daan=$_POST["daan"];
 9 $kemu=$_POST["kemu"];
10 $nandu=$_POST["nandu"];
11 $type=$_POST["type"];
12 
13 //修改题目
14 $sql1="update timu set name='{$name}',daan='{$daan}',kemu='{$kemu}',nandu='{$nandu}',type='{$type}' where code='{$code}'";
15 
16 if($db->Query($sql1,1))
17 {
18     //删除选项
19     $sdxx = "delete from xuanxiang where timu='{$code}'";
20     $db->Query($sdxx,1);
21     
22     if(!empty($_POST["A"]))
23     {
24         $a=$_POST["A"];
25         $sqla="insert into xuanxiang values('','{$a}','A','{$code}')";
26     
27         $db->Query($sqla,1);
28     }
29     
30     if(!empty($_POST["B"]))
31     {
32         $b=$_POST["B"];
33         $sqlb="insert into xuanxiang values('','{$b}','B','{$code}')";
34         $db->Query($sqlb,1);
35     }
36     
37     if(!empty($_POST["C"]))
38     {
39         $c=$_POST["C"];
40         $sqlc="insert into xuanxiang values('','{$c}','C','{$code}')";
41         $db->Query($sqlc,1);
42     }
43     
44     if(!empty($_POST["D"]))
45     {
46         $d=$_POST["D"];
47         $sqld="insert into xuanxiang values('','{$d}','D','{$code}')";
48         $db->Query($sqld,1);
49     }
50     
51     header("location:main.php");
52     }
53 
54 else
55 {
56     echo "修改失败!";
57     }
View Code

 运行修改页面:

posted @ 2016-06-05 18:32  陌上初薰  阅读(432)  评论(0编辑  收藏