• 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、完成    ^-^

  • 相关阅读:
    第三百二十八天 how can I 坚持
    第三百二十七天 how can I 坚持
    第三百二十六天 how can I 坚持
    第三百二十五天 how can I 坚持
    第三百二十四天 how can I 坚持
    第三百二十二天 how can I 坚持
    第三百二十一天 how can I 坚持
    第三百零八至三百二十天 how can I 坚持
    POJ 2346:Lucky tickets
    POJ 3461:Oulipo
  • 原文地址:https://www.cnblogs.com/little-rock/p/7698086.html
Copyright © 2020-2023  润新知