5分钟做一个php的增删改查案例

增删改查案例

PHP基础入门完成了,自己做了一个签到系统,用于可活动考勤,班级考勤等,主要是在校园中自己有时候需要使用,最近刚好学了PHP+MySQL的知识,于是自己构建了一个简单的考勤签到系统。这个案例主要实现的原理就是通过PHP执行SQL语句,更改数据库中的内容。接下来我们就来看看是怎么样实现的。

1、先看效果图:

添加信息的列表:
在这里插入图片描述
学生签到页面
在这里插入图片描述

2、代码

index.php的代码,主要将数据库的信息渲染在页面,这里通过PHP + H5 结合起来,然后通过循环,将数据库中存在的信息,经过模板渲染出来。如果学过JavaWeb的同学应该知道JSP,这个php就是类似JSP的方式将数据循环展示在页面中。

<?php
    # 连接数据库
    require_once "database.php";

?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <title>学生考勤签到系统</title>
    <style>
        body {
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="container">
    <h1 style="text-align: center;margin: 30px 0">学生考勤签到系统</h1>
    <form action="" method="get">
        <div>
            <input type="text"><input type="submit" value="查询">
        </div>
    </form>
    <form action="reset.php" method="get">

        <div style="float: right">
            <input  onclick="return alert('确定重置?')" name="reset"  type="submit" value="重置">
        </div>
    </form>

    <table class="table table-hover">
        <thead>
        <tr>
            <th>班级</th>
            <th>姓名</th>
            <th>电话</th>
            <th>状态</th>
            <th>签到</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php
         # 查询所有数据的sql语句
        $sql = "SELECT * FROM `students`";
        # 执行sql
        $result = $conn->query($sql);
        ?>
        <?php
            # for循环执行sql语句,边循环边赋值
            while ($row = $result->fetch_assoc()) {
                # var_dump($row);

        ?>
        <tr>
            <td><?php echo $row["classess"]; ?></td>
            <td><?php echo $row["name"]; ?></td>
            <td><?php echo $row["phone"]; ?></td>
            <td><?php echo $row["state"]; ?></td>
            <td>

                    <a type="submit" onclick="return confirm('是否已到场?')" href="updata.php?id=<?php echo $row['id']; ?>"><button>签到</button></a>

            </td>
            <td>
                <button>
                    <a onclick="return confirm('确定要删除吗?')" href="del.php?id=<?php echo $row['id']; ?>">删除</a>
                </button>
            </td>
        </tr>
        <?php
                 }
        ?>
        </tbody>
    </table>
    <div class="text-center">
        <button>
            <a href="add.html">添加学生</a>
        </button>

    </div>
</div>

</body>
</html>

3、 添加学生信息页面:

为了快速搭建这个页面,我采取的是bootstrap的框架进行搭建的,里面引入的是cdn的CSS文件,可以直接复制到自己的页面中,直接使用,也可以根据自己的需求,对其做出对应的改变。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <title>添加学生页面</title>
</head>
<body>
<div class="container contentCenter col-md-6  col-xs-12 col-lg-12" style="justify-content: center;">
    <h1 style="margin: 50px auto; text-align: center">活动参与成员登记表</h1>
    <form action="add.php" method="get">
        <div class="form-group">
            <label for="exampleInputEmail1">班级:</label>
            <input type="text" required name="classess" class="form-control" id="exampleInputEmail1" placeholder="请输入你所在的班级">
        </div>
        <div class="form-group ">
            <label for="exampleInputPassword1">姓名:</label>
            <input type="text" required name="username" class="form-control" id="exampleInputPassword1" placeholder="请输入姓名">
        </div>
        <div class="form-group ">
            <label for="exampleInputPassword2">电话:</label>
            <input type="tel" required name="telphone" class="form-control" id="exampleInputPassword2" placeholder="请输入电话号码">
        </div>

        <div>
            <input  type="hidden" name="state" value="未签">
        </div>
            <button type="submit" class="btn btn-default" onclick="return confirm('信息确定无误吗?')">确定参加</button>

    </form>
</div>
</body>
<script>

</script>
</html>

4、php代码块

在这里分享:添加、删除、数据库配置三部分的代码,其中删除和修改作用是一样的,根据获取 id 值进行删除和修改的操作。添加的页面,可以通过phpStudy软件中的phpMyAdmin工具自动生成添加的代码:

在这里插入图片描述
增加学生信息的的代码:

add.php

<?php
// 班级
$classess = $_GET['classess'];
// 姓名
$username = $_GET['username'];
// 电话
$telphone = $_GET['telphone'];
// 状态
$state = $_GET['state'];

// 连接数据库
require_once "database.php";

// 插入语句
$sql = "添加的sql语句放置在这里";


// 执行插入语句
if($conn->query($sql))
{
    header("location:add.html");
//删完回去表页面
}
else{
    echo "参与失败";
}

// 回到首页
header("location:add.html");
?>

数据库的配置文件:

database.php

<?php
// 面向对象的方式
$conn = new mysqli("自己本机地址","数据库账号","数据库密码", "数据库名");
// 判断是否连到数据库
if ($conn -> connect_error) {
    die("连接失败");
}

删除的php文件

del.php

<?php
// 获取id
$id = $_GET["id"];

// 连接数据库
require_once "database.php";

// 执行要删除的sql语句
$sql = "DELETE FROM `students` WHERE `students`.`id` = $id";

if($conn->query($sql)) //执行sql语句
{
    header("location:index.php");
//删完回去表页面
}
else{
    echo "删除失败";
}

// 回到首页
header("Location:index.php");

模板差不就到这里了,大家可以根据自己的需要进行增添功能。我后端学的是Java,在学servlet的时候写过一个类似这样的增删改查案例,现在了解了PHP , 发现PHP更为简单,而且不需要配置TomCat服务器。PHP开发中小型的系统,相对Java来说,比较快速一点(这里没有说Java不好,因为我主学习的也是Java)。

语言没有好坏之分,这个案例是自己了解PHP写的,**仅供参考学习,**往后自己学习Java的笔记会迁到博客中,分享自己的踩坑日记,一起为自己的程序人生Java。

  • 6
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值