zoukankan      html  css  js  c++  java
  • 6月4 Smarty练习增删改

    练习Smarty的增删改所需要用到的数据库名称:timu,xuanxiang,kemu,nandu,leixing,然后使用smarty模板将前端后后台分割开来:

    主页后台页面:zhupm.php

    <?php
    //将题目表显示在页面
    include("../init.inc.php");
    
    include("../DBDA.php");
    $db = new DBDA();
    
    $sql = "select * from timu";
    $attr = $db->Query($sql);
    
    $smarty->assign("shuju",$attr);
    
    $smarty->display("zhu0603pm.html");
    View Code

    主页前段显示页面:zhu0603pm.html

    <html>
    <head>
    <meta charset="utf-8" />
    </head>
    <title>Smarty的增删改查</title>
    
    <body>
    <h1>主页面</h1>
    <br />
    
    <table border="1" cellpadding="0" cellspacing="0" width="100%">
        <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="shanchu.php?code=<{$v[0]}>">删除</a>&nbsp;&nbsp;<a href="xiugai.php?code=<{$v[0]}>">修改</a></td> 
    </tr>
    <{/foreach}>
    </table>
    <br>
    <a href="tianjia.php">添加数据</a>
    
    
    </body>
    </html>
    View Code

    添加后台页面:tianjia.php

    <?php
    //添加数据
    include("../init.inc.php");
    
    include("../DBDA.php");
    $db = new DBDA();
    
    $sql = "select * from kemu";
    $attr = $db->Query($sql);
    
    $sqlnd = "select * from nandu";
    $attrnd = $db->Query($sqlnd);
    
    $sqllx = "select * from leixing";
    $attrlx = $db->Query($sqllx);
    
    $smarty->assign("kemu",$attr);
    $smarty->assign("nandu",$attrnd);
    $smarty->assign("type",$attrlx);
    
    $smarty->display("tianjia03pm.html");
    View Code

    添加处理页面:tianjiachuli.php

    <?php
    
    include("../DBDA.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}')";
    
    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:tianjia.php");
    
    }
    else
    {
        echo "添加失败";    
    }
    View Code

    添加前段显示页面:tianjia03pm.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>Smarty的增删改查</title>
    </head>
    
    <body>
    <h1>添加数据</h1>
    <form action="tianjiachuli.php" method="post">
    
    <div>输入题目名称:<input type="text" name="name" /></div><br />
    
    <div>选项A:&nbsp;&nbsp;<input type="text" name="a" /></div>
    <div>选项B:&nbsp;&nbsp;<input type="text" name="b" /></div>
    <div>选项C:&nbsp;&nbsp;<input type="text" name="c" /></div>
    <div>选项D:&nbsp;&nbsp;<input type="text" name="d" /></div><br />
    
    
    
    <div>添加答案:&nbsp;&nbsp;<input type="text" name="daan" /></div><br />
    
    
    <div>选择科目:&nbsp;&nbsp;
            <select name="kemu">
                <{foreach $kemu as $v}>
                    <option value="<{$v[0]}>"><{$v[1]}></option>
                <{/foreach}>
            </select>
    </div><br />
    
    
    <div>选择题目难度:
            <select name="nandu">
                <{foreach $nandu as $v}>
                    <option value="<{$v[0]}>"><{$v[1]}></option>
                <{/foreach}>
            </select>
    </div><br />
    
    
    <div>选择题目类型:
            <select name="type">
                <{foreach $type as $v}>
                    <option value="<{$v[0]}>"><{$v[1]}></option>
                <{/foreach}>
            </select>
    </div>    <br />
    
    <input type="submit" value="提交" />
    
    </form>
    <br />
    <a href="zhupm.php">返回主页</a>
    </body>
    </html>
    View Code

    删除后台页面:shanchu.php

    <?php
    
    include("../DBDA.php");
    $db = new DBDA();
    $code = $_GET["code"];
    
    $sql = "delete from xuanxiang where timu = '{$code}'";
    $db->Query($sql,1);
    
    $sql = "delete from timu where code = '{$code}'";
    $db->Query($sql,1);
    
    header("location:zhupm.php");
    View Code

    修改后台页面:xiugai.php

    <?php
    include("../init.inc.php");
    
    include("../DBDA.php");
    $db = new DBDA();
    $code = $_GET["code"];
    
    $sqltm = "select * from timu where code='{$code}'";
    $attrtm = $db->Query($sqltm);
    
    $sqlxx = "select * from xuanxiang where timu='{$code}'";
    $attrxx = $db->Query($sqlxx);
    
    $sqlkm = "select * from kemu";
    $attrkm = $db->Query($sqlkm);
    
    $sqlnd = "select * from nandu";
    $attrnd = $db->Query($sqlnd);
    
    $sqllx = "select * from leixing";
    $attrlx = $db->Query($sqllx);
    
    $smarty->assign("code",$code);
    $smarty->assign("timu",$attrtm);
    $smarty->assign("xuan",$attrxx);
    $smarty->assign("kemu",$attrkm);
    $smarty->assign("nandu",$attrnd);
    $smarty->assign("type",$attrlx);
    
    $smarty->display("xiugai03pm.html");
    View Code

    修改处理页面:xiugaichuli.php

    <?php
    include("../init.inc.php");
    
    include("../DBDA.php");
    $db = new DBDA();
    
    $code = $_POST["code"];
    $name = $_POST["name"];
    $daan = $_POST["daan"];
    $kemu = $_POST["kemu"];
    $nandu = $_POST["nandu"];
    $type = $_POST["type"];
    
    //修改题目
    $sqltm = "update timu set name='{$name}',daan='{$daan}',kemu='{$kemu}',nandu='{$nandu}',type='{$type}' where code='{$code}'";
    //echo $sqltm;
    if($db->Query($sqltm,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:zhupm.php");
    }
    else
    {
        echo "修改失败";    
    }
    View Code

    修改前段显示页面:xiugai03pm.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>Smarty的增删改查</title>
    </head>
    
    <body>
    <h1>修改数据</h1>
    <form action="xiugaichuli.php" method="post">
    <input type="hidden" name="code" value="<{$timu[0][0]}>" />
    <div>输入题目名称:<input type="text" name="name" value="<{$timu[0][1]}>" /></div><br />
    
    <{foreach $xuan as $v}>
        <div>选项名称:<{$v[2]}><input type="text" value="<{$v[1]}>" name="<{$v[2]}>" ></div>
    <{/foreach}>
    
    <div>添加答案:&nbsp;&nbsp;<input type="text" name="daan" value="<{$timu[0][2]}>"/></div><br />
    
    
    <div>选择科目:&nbsp;&nbsp;
            <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><br />
    
    
    <div>选择题目难度:
            <select name="nandu">
                <{foreach $nandu as $v}>
                    <{if $v[0]==$timu[0][4]}>
                        <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option>
                    <{else}>
                        <option value="<{$v[0]}>"><{$v[1]}></option>
                       <{/if}>
                <{/foreach}>
            </select>
    </div><br />
    
    
    <div>选择题目类型:
            <select name="type">
                <{foreach $type as $v}>
                    <{if $v[0]==$timu[0][5]}>
                    <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option>
                    <{else}>
                    <option value="<{$v[0]}>"><{$v[1]}></option>
                       <{/if}>
                <{/foreach}>
            </select>
    </div>    <br />
    <input type="submit" value="提交" />
    
    </form>
    <br />
    <a href="zhupm.php">返回主页</a>
    </body>
    </html>
    View Code

     

  • 相关阅读:
    std thread
    windows更新包发布地址
    How to set up logging level for Spark application in IntelliJ IDEA?
    spark 错误 How to set heap size in spark within the Eclipse environment?
    hadoop 常用命令
    windows 安装hadoop 3.2.1
    windows JAVA_HOME 路径有空格,执行软连接
    day01MyBatisPlus条件构造器(04)
    day01MyBatisPlus的CRUD 接口(03)
    day01MyBatisPlus入门(02)
  • 原文地址:https://www.cnblogs.com/Duriyya/p/5566191.html
Copyright © 2011-2022 走看看