php 数据库内容增删改查----增

首先,建立一个主页面(crud.php)

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<?php

$db = new Mysqli("localhost","root","root","student");
!empty(mysqli_connect_error())?die("连接错"):"";

$sql = "select * from student";

$data = $db->query($sql)->fetch_all();

//var_dump($data);

<table border="1">
    <tr>
        <td>ID</td>
        <td>姓名</td>
        <td>性别</td>
        <td>出生年月</td>
        <td>班级</td>
        <td>删除</td>
        <td>修改</td>
    </tr>


    <?php
    foreach ($data as $i){
        if ($i[2] == 1){
            $i[2] = "";
        }
        else if ($i[2] == 0){
            $i[2] = "";
        }
        else{
            $i[2] = "保密";
        }
        echo "<tr><td>{$i[0]}</td><td>{$i[1]}</td><td>{$i[2]}</td><td>{$i[3]}</td><td>{$i[4]}</td><td><a href='dele.php?id={$i[0]}'>&nbsp;删除&nbsp;</a></td><td><a href='edit.php'>&nbsp;修改&nbsp;</a></td><tr>";
    }
    ?>
<!--    2-->


</table>
<a href="add.php">新增用户</a>

</body>
</html>
复制代码

一、增加

1、建立add.php ,插入表单

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" action="addpost.php">
        <input type="text" name="Sno" placeholder="id">

        <input type="text" name="Sname" placeholder="姓名">

        <input type="radio" name="Ssex" value="1" id="man"><label for="man">男</label>
        <input type="radio" name="Ssex" value="0" id="woman"><label for="woman">女</label>

        <input type="text" name="Sbirthday" placeholder="出生年月">

        <input type="text" name="Class" placeholder="班级">

        <input type="submit" value="提交">
    </form>

</body>
</html>
复制代码

2、在addpost.php里做数据库增加处理

复制代码
<?php

$id = $_POST['Sno'];
$name = $_POST['Sname'];
$sex = $_POST['Ssex'];
$birthday = $_POST['Sbirthday'];
$class = $_POST['Class'];

$db = new Mysqli("localhost","root","root","student");
$sql = "insert into student.student VALUES ('{$id}','{$name}','{$sex}','{$birthday}','{$class}',0)";

//var_dump($db->query($sql));
if ($db->query($sql)){
    header("location:crud.php");
}else{
    header("location:add.php");
}
复制代码

测试一下:

1、主页

2、点击 增加用户  增加内容

3、点击 提交  ,看一下

4、完成    ^-^

原文地址:https://www.cnblogs.com/GP1014336455/p/7699333.html