• 2018-2-5 数据库表格的增删改操作


    将数据库中表格连接到php中显示,并进行增删改的操作:

    这是一个关于老师信息的表格,首页如下:

    原数据库内容:

    代码如下:文件名为index1.php。

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>这是首页-展示老师页表</title>
    </head>
    
    <body>
    <?php
        //创建数据库 连接备用
        $db=new MySQLi("localhost","root","","z_stu");
        !mysqli_connect_error() or die("连接失败");
        $db->query("set names utf8");    //设置数据库字符集
        //获取teacher表的内容
        $sql="select * from teacher";
        $result=$db->query($sql);        //转成结果集
        $attr=$result->fetch_all();        //以数组形式输出
    ?>
    <table border="1" width="80%">
        <caption>老师表</caption>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>生日</th>
            <th>职称</th>
            <th>所在系</th>
            <th>操作</th>
        </tr>
        <?php
        foreach($attr as $v){ ?>
            <tr>
                <td><?php echo $v[0]; ?></td>
                <td><?php echo $v[1]; ?></td>
    <!--            三目运算符,判断性别-->
                <td><?php echo $v[2] == "男" ? "男" : "女"; ?></td>    
                <td><?php echo substr($v[3],0,10); ?></td>
                <td><?php echo $v[4]; ?></td>
                <td><?php echo $v[5]; ?></td>
                <td><!--<a href="chuli/delete.php?uid='<?php echo $v[0]; ?>' ">
                        <button>删除</button>
                    </a>-->
                    <!--给删除按钮添加事件,跳转页面,获取编号$v[0]的值-->
                    <form action="chuli/delete.php" method="post">
                        <input type="hidden" name="uid"
                            value="<?php echo $v[0]; ?>">
                        <button class="btn">删除</button>    <!--删除按钮-->
                    </form>
                    <a href="update.php?type=update&tno=<?php echo $v[0]; ?>">
                        <button>编辑</button>
                    </a>
                    
                </td>
            </tr>
        <?php } ?>
    </table>
    <a href="update.php?type=add">
        <button>添加数据</button>
    </a>
    <br>
    </body>
    </html>

    右侧的删除,编辑按钮可以对本行数据进行操作,底部添加数据,可以添加一行的老师信息。

    由于编辑信息和添加数据都是对老师信息的操作,所以可以用一个页面操作,即update.php,接收数据,都用insert.php

    编辑数据代码如下:

    <!--这是编辑数据的页面-->
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>编辑老师信息</title>
    </head>
    
    <body>
    <?php
        //连接数据库
        $db=new MySQLi("localhost","root","","z_stu");
        !mysqli_connect_error() or die("连接失败");
        //设置字符集
        $db->query("set names utf8");
        $type=$_GET["type"];
        if($type=="update"){
            //$sql语句获取 $_GET["tno"] 这一行的数据
            $sql="select * from teacher where tno='".$_GET["tno"]."'";
            //执行$sql语句
            $result=$db->query($sql); 
            $attr=$result->fetch_row();
        }
        //设定数组,数据后面选择使用 68行 和 82行
        $proname=array("助教","副教授","教授");
        $dpname=array("计算机系","电子工程系");
    ?>
    <fieldset>
        <legend><?php echo $type=="update" ? "编辑":"添加"; ?>数据</legend>
    <!--    建立form表单,设定完成后跳转的页面和数据接收方式-->
        <form action="chuli/insert.php" method="post">
    <!--    建立input,设定type标记,便于insert.php中if语句辨认-->
                <input type="hidden" name="type" value="<?php echo $type; ?>">
            <table>
                <tr>
                    <td>编号:</td>
                    <td>
                        <input type="text" name="tno" value="<?php echo $attr[0]==null ? "": $attr[0]; ?>">
                    </td>
                </tr>
                <tr>
                    <td>姓名:</td>
                    <td>
                        <input type="text" name="tname" value="<?php echo $attr[1]==null ? "": $attr[1]; ?>">
                    </td>
                </tr>
                <tr>
                    <td>性别:</td>
                    <td>
                    <!--
                    第一种:
                    <?php if($attr[2] == "男"){?>
                        <input type="radio" name="tsex" value="男" checked><input type="radio" name="tsex" value="女"><?php }else{?>
                        <input type="radio" name="tsex" value="男" ><input type="radio" name="tsex" value="女" checked><?php }?>
                    -->
                    <!--第二种-->
                    <input type="radio" name="tsex" value="男" <?php echo $attr[2] == "男"? "checked":"" ?> ><input type="radio" name="tsex" value="女" <?php echo $attr[2] == "女"? "checked":"" ?> ></td>
                </tr>
                <tr>
                    <td>出生年月:</td>
                    <td>
                        <input type="text" name="tbirthday" value="<?php echo $attr[3]==null ? "": $attr[3]; ?>">
                    </td>
                </tr>
                <tr>
                    <td>职称:</td>
                    <td>
                        <select name="prof">
                            <?php foreach($proname as $k=>$v)
                                if($v==$attr[4]){
                                    echo"<option selected>$v</option>";
                                }else{
                                    echo"<option>$v</option>";
                                }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>所在系:</td>
                    <td>
                        <select name="depart">
                            <?php foreach($dpname as $k=>$v)
                                if($v==$attr[5]){
                                    echo"<option selected>$v</option>";
                                }else{
                                    echo"<option>$v</option>";
                                }
                            ?>
                        </select>
                    </td>
                </tr>
                
            </table>
            <button>提交</button>
        </form>
    </fieldset>
    </body>
    </html>

    点击编辑按钮,进入编辑数据页面:

    点击添加数据,进入添加数据页面:

    接收数据代码如下:

    <?php
    $type=$_POST["type"];
    $tno=$_POST["tno"];    //编号
    $tname=$_POST["tname"];//姓名
    $tsex=$_POST["tsex"];//性别
    $tbirthday=$_POST["tbirthday"];//生日
    $prof=$_POST["prof"];//职称
    $depart=$_POST["depart"];//所在系
    
    //连接数据库
    $db=new MySQLi("localhost","root","","z_stu");
    !mysqli_connect_error() or die("连接失败");
    $db->query("set names utf8");
    //$sql语句,获取数据
    //判断路径,从哪里来
    if($type=="add"){    //如果是从add.php来,执行以下语句(添加数据)
        $sql="insert into teacher
            ("."tno,"."tname,"."tsex,"."tbirthday,"."prof,"."depart".") values("."'$tno',"."'$tname',"."'$tsex',"."'$tbirthday',"."'$prof',"."'$depart')";
    }else if($type=="update"){    //如果是从update.php来,执行以下语句(编辑数据)
        $sql="update teacher set tname='$tname',tsex='$tsex',tbirthday='$tbirthday',prof='$prof',depart='$depart' where tno='$tno'";
        }
    
    $result=$db->query($sql); //执行$sql语句
    header("location:../index1.php");//跳转到index.php文件
    ?>

    删除数据(删除本行信息),代码如下:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>删除数据</title>
    </head>
    
    <body>
    <?php
        //这个页面是处理文件夹下面的删除.php
        //连接数据库
        $db=new MySQLi("localhost","root","","z_stu");
        !mysqli_connect_error() or die("连接失败");
        $db->query("set names utf8");
        $uid=$_POST["uid"];
        //$sql语句删除tno='$uid'"这一行的数据
        $sql="delete from teacher where tno='$uid'";
        $result=$db->query($sql); //执行$sql语句
        //跳转到index1.php页面
        header("location:../index1.php");
        
    ?>
    </body>
    </html>
  • 相关阅读:
    整数因子分解问题(递归分治法、动态规划)
    背包问题(动态规划 C/C++)
    划分问题(Java 动态规划)
    算法:Common Subsequence(动态规划 Java 最长子序列)
    算法:矩阵连乘(Java)动态规划
    Frogs‘ Neighborhood(POJ 1659 C/C++)
    算法:线性时间选择(C/C++)
    sort(hdu oj 1425)计数排序和快速排序
    快速排序(递归和分治)
    a^b(取模运算)
  • 原文地址:https://www.cnblogs.com/dns6/p/8418812.html
Copyright © 2020-2023  润新知