zoukankan      html  css  js  c++  java
  • php显示表中数据及删除数据

    一、连接数据库想要在页面显示数据库表中的内容,并想要随时删除不需要的数据,是我们经常要用到的,我们首先新建一个页面,在Body里面建一个表:

    <table width="100%" border="1" cellpadding="0" cellspacing="0">
        <tr>
            <td>代号</td>
            <td>密码</td>
            <td>姓名</td>
            <td>性别</td>
            <td>生日</td>
            <td>操作</td>
        </tr>
    </table>

    二、然后连接数据库,循环遍历出表中的内容,由于表中涉及到性别是bool类型,所以需要转为男女的格式:

     <?php
        $db = new MySQLi("localhost","root","123","text_0306");
        $sql = "select * from users";
        $result = $db->query($sql);
        $arr = $result->fetch_all();
        foreach($arr as $v)
        {
            //修改性别
            $sex = $v[3]?"":"";
            
            //修改民族
            /*$sql1 = "select name from nation where code='{$v[3]}'";
            $r1 = $db->query($sql1);
            $a1 = $r1->fetch_row();*/
            
            echo "<tr>
            <td>{$v[0]}</td>
            <td>{$v[1]}</td>
            <td>{$v[2]}</td>
            <td>{$sex}</td>
            <td>{$v[4]}</td>
            <td><a href='del.php?uid={$v[0]}' onclick="return confirm('确认删除么?')">删除</a></td>
        </tr>";//这里用到a标签和点击事件,点击事件后面不只可以跟函数,还可以跟js代码
        }
        ?>

    三、上面页面完成后需要重新建立一个提取数据的页面,并在删除成功后跳转页面:

    <?php
    $uid = $_GET["uid"];//提取数据
    
    $db = new MySQLi("localhost","root","123","text_0306");
    $sql = "delete from users where uid='{$uid}'";
    if($db->query($sql))
    {
        //跳转页面
        header("location:main.php");
        /*echo "<script>window.location.href='main.php'</script>";这是第二种跳转方式,主要用于文件不在同一目录下时
    }
    else
    {
        echo "删除失败!";
    }
  • 相关阅读:
    Luogu p2672 推销员 (贪心)
    HDU 1548 A strange lift (暴力搜索)
    HDU 1258 Sum it up (dfs)
    HDU 1087 FatMouse and Cheese (记忆化搜索)
    Beautiful Numbers(牛客)
    E. Product Oriented Recurrence(矩阵快速幂+欧拉降幂)
    Super A^B mod C FZU
    bitset入门 简单瞎搞题
    D. Dirty Deeds Done Dirt Cheap(思维)
    大家一起来数二叉树吧(DP)
  • 原文地址:https://www.cnblogs.com/mengshenshenchu/p/6770312.html
Copyright © 2011-2022 走看看