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>

时间: 2024-11-05 11:50:37

Smarty练习增删改的相关文章

6月5日 smarty应用(增删改查)

smarty可以实现HTML与php代码的分离,之前我们用php代码做过数据的增删改查,现在我们运用smarty来实现这些功能,并用分页显示 查询; 主页面,以表格的形式显示数据及需要的操作:one.html <body> <h1>主页面</h1> <form action="one.php" method="get"> <div>题目名称: <input type="text"

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

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

Python 模拟SQL对文件进行增删改查

1 #!/usr/bin/env python 2 # _*_ coding:UTF-8 _*_ 3 # __auth__: Dalhhin 4 # Python 3.5.2,Pycharm 2016.3.2 5 # 2017/05/15 6 7 import sys,os 8 9 def where(dbfile,where_list): #条件是一个list 10 11 def and_or_where(sub_where_list): 12 '''获取and或同时含有and.or关键字的条

【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建

数据库的创建和sql语句增删改查 1. 加载驱动. 2. 连接数据库. 3. 操作数据库. 创建表: create table person( _id integer primary key, name varchar(20), age integer ); 添加: insert into person(name, age) values('lisi', 19); 删除: delete from person where _id = 1; 修改: update person set name =

ssm框架搭建+easyui增删改查实现

闲来无事,看了看别人的博客文档也跟着敲了敲,特地记录下来,方便以后学习: spring版本:4.0.6 mybatis版本:3.2.5 所有jar包打包下载:http://pan.baidu.com/s/1qLEaU 1.项目目录结构 其中,controller包下存放控制层文件,dao下存放各个model类相关的数据库操作接口,entity下放置各种model类,mappers下放置各个dao对应的映射文件,service服务层就不说了,放置各种service接口,impl是其具体实现类. 2

【Android】Sqlite数据库增删改查

Android系统内置一个Sqlite数据库,如果app需要使用Sqlite数据库数据库存储数据,Android会为此app生成一个.db文件.这个数据库在data/data/<package_name>/databases里面,其中<package_name>为该安卓app的工程包名,这个目录必须root后才能看到.在Windows,单机的应用程序,存储数据,基本放到一个文件里面,正如游戏的存档,基本就是把当前的游戏状态存到一个用户很难找到的文件里面.每次存档读档就是一个从这个存

夺命雷公狗---Thinkphp----12之文章的增删改查(图片上传和关联查询)

我们由于表分析的不够完善,所以我们来加多一个tid的字段,到时候主要目的是为了更好的遍历出文章是属于那个分类下的,表如下所示: 那么下一步我们就开始创建一个ArticleController.class.php的控制器,用来管理文章的增删改查操作,所以我们开始第一步来实现文章的添加,我们先来实现他的增加功能: public function add(){ if(IS_POST){ $data['title'] = I('title'); $data['tid'] = I('tid'); $dat

MyBatis增删改查模板

1. 首先,和Spring整合一下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springfra

MongoDB(2): 增删改操作

附加命令: 1.进入前端操作命令 ./mongo [ip:端口] 说明:默认会自动选本地,端口27017 2.显示所有的库 > show dbs;   或者 show databases; 3.选择库 > use 库名; 4.显示库所有的集合 > show collections;  或者 show tables; 5.显示当前使用的库 > db; 一.操作数据库.文档 1.1.数据库操作 1.创建数据库:MongoDB没有专门创建数据库的语句,可以使用"use"