PHP-------smaty 增删改查

                                                             smaty 增删改查

smarty-----main文件夹,是放php文件的--------templates文件夹是放html文件的 

我们访问的页面是php页面,PHP页面需要使用smarty模板,需要把smarty模板路口文件或者叫配置文件引入。include("../init.inc.php");。这个php页面在访问是需要打出一个页面让用户看到页面的内容,所以要调用smarty对象里面的方法$smarty->display( 模板页面的名字,html);.

 

登陆
main-------denglu.php
 1 <?php
 2 include("../init.inc.php");
 3 $smarty->display("denglu.html");
 4 
 5 
 6 templates-----denglu.html
 7 <title>登陆界面</title>
 8 </head>
 9 <body>
10  <center>
11 <h1>登陆界面</h1>
12 <table width="20%" border="0" cellpadding="0" cellspacing="0">
13 <tr>
14 <form action="dengluchuli.php" method="post">
15 <tr><td style="font-size:23px">用户名</td><td><input type="text" name="yonghuming" style=" width:170px; height:28px" /></td></tr>
16 <tr><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr>
17 <tr><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr>
18 <tr><td  style="font-size:23px">密码</td><td><input type="password" name="mima" style=" width:170px; height:28px"/></td></tr>
19 <tr><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr>
20 <tr><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr> 
21  </table>
22  <center><input type="submit" value="登陆" /><center> 
23 </form>
24 </center>
25 </body>
26 </html>

 

main-------dengluchuli.php

 1 <?php
 2 session_start();    //开启session
 3 include("../DBDA.php");   //要查数据库就要引用DBDA.PHP类
 4 $db = new DBDA();
 5 $yonghuming = $_POST["yonghuming"];
 6 $mima = $_POST["mima"];
 7 $sql = "select pwd from users where uid='{$yonghuming}'";    //根据密码查用户名
 8 $mima1 = $db->StrQuery($sql);    //查的是字符串,就一条数据
 9 if($yonghuming!="" && $mima!="")   //用户名和密码不能为空
10 {
11     if($mima==$mima1)  //去匹配用户传的密码和数据库查到的密码是否一致
12     {
13         $_SESSION["yonghuming"]=$yonghuming;
14         header("location:zhuyemian.php");
15     }
16     else
17     {
18            header("location:denglu.php");
19     }
20 }
21 else
22     {
23            header("location:denglu.php");
24     }

 

主页面
main----zhuyemian.php
 1 <?php
 2 session_start();
 3 include("../init.inc.php");
 4 include("../DBDA.php");
 5 include("page.class.php");
 6 
 7 $db = new DBDA();
 8 
 9 if(empty($_SESSION["yonghuming"]))
10 {
11     header("location:denglu.php");
12     exit;
13 }
14 
15 $tj = " 1=1 ";
16 $name = "";
17 if(!empty($_GET["name"]))
18 {
19     $name = $_GET["name"];
20     $tj = " name like '%{$name}%' ";
21 
22 }
23 $ztj = " where {$tj}";
24 
25 //查出总条数
26 $szs = "select count(*) from info ".$ztj;
27 $zs = $db->StrQuery($szs);
28 
29 //造分页类对象
30 $page = new page($zs,3);
31 
32 $yonghuming = $_SESSION["yonghuming"];
33 //根据用户名查姓名
34 $sql = "select name from users where uid='{$yonghuming}'";
35 $xingming = $db->StrQuery($sql);
36 
37 //查info表
38 $sql1 = "select * from info ".$ztj.$page->limit;
39 $a = $db->Query($sql1);
40 
41 
42 //取分页信息
43 $xinxi = $page->fpage();
44 
45 $smarty->assign("xingming",$xingming);
46 $smarty->assign("a",$a);
47 $smarty->assign("xinxi",$xinxi);
48 $smarty->assign("name",$name);
49 
50 
51 $smarty->display("zhuyemian.html");

 

templates----zhuyemian.html

 1 <title>主页面(用户管理)</title>
 2 <style type="text/css">
 3 *{ margin:0px auto; padding:0px; font-family:微软雅黑}
 4 #main{ width:1320px;}
 5 #top{ width:1320px; height:100px; background-color:#09F; line-height:100px; vertical-align:middle; color:#FFF; font-weight:bold}
 6 #neirong{ margin-top:30px}
 7 </style><!--设置垂直居中有两个条件::::垂直要设置div的高度line-height:100px;,和垂直居中居中vertical-align:middle-->
 8 </head>
 9 <body>
10 <center><h1>主页面(用户管理)</h1></center>
11 <div id="main"></div>
12 <div id="top">
13 欢迎登录:<{$xingming}>
14 </div>
15 
16 <div id="neirong">
17 <form action="zhuyemian.php" method="get">
18 <div>
19     请输入姓名:
20     <input type="text" name="name" value="<{$name}>" />
21     <input type="submit" value="查询" />
22 </div>
23 </form>
24 
25 <table width="100%" border="1" cellpadding="0" cellspacing="0">
26 <tr bgcolor="#00FF99"> 
27 <td>代号</td>
28 <td>姓名</td>
29 <td>性别</td>
30 <td>民族</td>
31 <td>出生日期</td>
32 <td>操作</td>
33 </tr>
34 
35 <{foreach $a as $v}>
36 <{if $v@index%2==0}>
37 <tr>
38 <td><{$v[0]}></td>
39 <td><{$v[1]}></td>
40 <td><{$v[2]|sexname}></td>
41 <td><{$v[3]|nationname}></td>
42 <td><{$v[4]}></td>
43 <td><a href="shanchu.php?code=<{$v[0]}>" onclick="return confirm('确定要删除吗?')">删除</a>
44 <a href="xiugai.php?code=<{$v[0]}>">修改</a>
45 <input type="button" value="查看详情" />
46 
47 </td>
48 </tr>
49 <{else}>
50 <tr bgcolor="#FF99FF">
51 <td><{$v[0]}></td>
52 <td><{$v[1]}></td>
53 <td><{$v[2]|sexname}></td>
54 <td><{$v[3]|nationname}></td>
55 <td><{$v[4]}></td>
56 <td><a href="shanchu.php?code=<{$v[0]}>" onclick="return confirm('确定要删除吗?')">删除</a>
57 <a href="xiugai.php?code=<{$v[0]}>">修改</a>
58 <input type="button" value="查看详情" />
59 </td>
60 </tr>
61 <{/if}>
62 <{/foreach}>
63 </table>
64 
65 <div><{$xinxi}></div>
66 </div>
67 
68 <div><a href="tianjia.php">添加数据</a><div>
69 </body>
70 </html>
 
添加页面
main-----tianjai.php
 1 <?php
 2 include("../init.inc.php");
 3 include("../DBDA.php");
 4 
 5  $db= new DBDA();
 6  $sql = "select * from nation";
 7  $attr = $db->Query($sql);
 8 $smarty->assign("minzu",$attr);
 9 $smarty->display("tianjia.html");
10 
11 templates----tianjai.html
12 <title>添加数据</title>
13 </head>
14 <body>
15 <h1>添加数据</h1>
16 <form action="tianjiachuli.php" method="post">
17 <div>代号:<input type="text" name="daihao" /></div>
18 <div>姓名:<input type="text" name="xingming" /></div>
19 <div>性别:<input type="radio" name="xingbie" value="1" checked="checked"/>20 <input type="radio" name="xingbie" value="0"/>女</div>
21 <div>民族:
22 <select name="minzu">
23 <{foreach $minzu as $v}>
24 <option value="<{$v[0]}>"><{$v[1]}></option>
25 <{/foreach}>
26 </select>
27 </div>
28 <div>出生日期:<input type="text" name="chusheng" /></div>
29 <div><input type="submit" value="添加数据" /></div>
30 </form>
31 </body>
32 </html>
33 
34 main-----tianjiachuli.php
35  <?php
36 include("../DBDA.php");
37 $db = new DBDA();
38 $daihao = $_POST["daihao"];
39 $xingming = $_POST["xingming"];
40 $xingbie = $_POST["xingbie"];
41 $minzu = $_POST["minzu"];
42 $chusheng = $_POST["chusheng"];
43 
44 $sql = "insert into info values('{$daihao}','{$xingming}','{$xingbie}','{$minzu}','{$chusheng}')";
45 
46 if($db->Query($sql,0))
47 {
48     header("location:zhuyemian.php");
49 
50 }
51 else
52 {
53     echo "添加失败";
54 
55     }

 

修改页面
main----xiugai.php
 1 <?php
 2 include("../init.inc.php");
 3 include("../DBDA.php");
 4 
 5 $db = new DBDA();
 6 $sql = "select * from nation";
 7 $attr = $db->Query($sql);
 8 
 9 $code = $_GET["code"]; 
10 $sxx = "select * from info where code='{$code}'";  
11 $sxx = $db->Query($sxx);
12 //var_dump($sxx);
13 $daihao = $sxx[0][0];
14 $xingming = $sxx[0][1];
15 $xingbie = $sxx[0][2];
16 $minzu = $sxx[0][3];
17 $chusheng = $sxx[0][4];
18 
19 $smarty->assign("minzu1",$attr);
20 
21 $smarty->assign("daihao",$daihao);
22 $smarty->assign("xingming",$xingming);
23 $smarty->assign("xingbie",$xingbie);
24 $smarty->assign("minzu",$minzu);
25 $smarty->assign("chusheng",$chusheng);
26 
27 $smarty->display("xiugai.html");

 

templates -----xiugai.html

 1 <title>无标题文档</title>
 2 </head>
 3 <body>
 4 <center>
 5 <h1>修改数据</h1>
 6 <form action="xiugaichuli.php" method="post">
 7 <div> <input type="hidden" name="daihao" value="<{$daihao}>"/></div>
 8 <div>姓名:<input type="text" name="xingming" value="<{$xingming}>"/></div>
 9 <br />
10 <div>性别:
11 <{if $xingbie}>
12 <input type="radio" name="xingbie" value="1" checked="checkde" />13 <input type="radio" name="xingbie" value="0" />女</div>
14 <{else}>
15 <input type="radio" name="xingbie" value="1" />16 <input type="radio" name="xingbie" value="0" checked="checked"/>女</div>
17 <{/if}>
18 <br />
19 
20 <div>民族:
21 <select name="minzu">
22 <{foreach $minzu1 as $v}>
23 <{if $minzu==$v[0]}>
24 <option value="<{$v[0]}>" selected="selected"><{$v[1]}></option>
25 <{else}>
26 <option value="<{$v[0]}>"><{$v[1]}></option>
27 <{/if}>
28 <{/foreach}>
29 </select>
30 </div>
31 <br />
32 <div>出生日期:<input type="text" name="chusheng" value="<{$chusheng}>"/></div>
33 <br />
34 <div><input type="submit" value="修改数据" /></div>
35 </form>
36 </center>
37 </body>
38 </html>

 

main-----xiugaichuli.php

 1 <?php
 2 include("../DBDA.php");
 3 $db = new DBDA();
 4 $daihao = $_POST["daihao"];
 5 $xingming = $_POST["xingming"];
 6 $xingbie = $_POST["xingbie"];
 7 $minzu = $_POST["minzu"];
 8 $chusheng = $_POST["chusheng"];
 9 
10 $sql = "update info set Name='{$xingming}',Sex={$xingbie},Nation='{$minzu}',Birthday='{$chusheng}' where Code='{$daihao}'";
11  $db->Query($sql,0);
12 
13  header("location:zhuyemian.php");

 

删除页面
main------shanchu.php
1 <?php
2 include("../DBDA.php");
3 $db= new DBDA();
4 $code = $_GET["code"];
5 
6 $sql = "delete from info where Code='{$code}'";
7 $db->Query($sql,0);
8 header("location:zhuyemian.php");

 

posted @ 2016-07-29 20:23  玉玉_312  阅读(918)  评论(0编辑  收藏  举报