• Smarty练习增删改




    <?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");

    主页前段显示页面: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>

    添加后台页面: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");

    添加处理页面: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 "添加失败";    
    }

    添加前段显示页面: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>

    删除后台页面: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");

    修改后台页面: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");

    修改处理页面: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 "修改失败";    
    }

    修改前段显示页面: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>

  • 相关阅读:
    [LeetCode] Reverse Linked List II 解题报告
    [LeetCode] Search for a Range 解题报告
    Git教程
    今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
    套接字的域和地址族
    套接口和I/O通信
    2010年第82届奥斯卡金像奖获奖影片名单
    恩信ERP7.10安装、定制和二次开发 (2)
    从 Google 代码库找到的好东西 [转]
    更自然的人机交互——2010微软技术节现场速递
  • 原文地址:https://www.cnblogs.com/kevin2016/p/5721634.html
Copyright © 2020-2023  润新知