zoukankan      html  css  js  c++  java
  • 如何对数据库进行增删改查

    一.先建一个AM.php文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>民族</td>
        <td>生日</td>
        <td>操作</td>
    
    
    </tr>
    <?php
    
        //造链接对象
        $db =new MySQLi("localhost","root","123456","xinjian");
        //判断
        !mysqli_connect_error() or die("链接失败");
        //写sql语句
        $sql ="select * from Info";
        //执行
        $result =$db->query($sql);
        //处理
        $attr = $result->fetch_all();
        for($i=0;$i<count($attr);$i++)
        {
            echo "<tr>";
            for($j=0;$j<count($attr[$i]);$j++)
            {
                echo "<td>{$attr[$i][$j]}</td>";
                }
            //删除,因为它指向的是同一个页面,不知道是删哪一个,所以后面手动写 告诉他删谁    
            echo "<td><a href='Delete.php?code={$attr[$i][0]}'>删除</a><a href='Update.php?code={$attr[$i][0]}'>修改</a></td>";//手动写
            echo "</tr>";
            }
    
    
    ?>
    </table>
    
    <br />
    <a href="Add.php"><input type="button" value="添加数据" /></a>
    
    </body>
    </html>

    二.创建一个Add.php文件,点击“添加数据“之后跳转到Add.php页面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    
    <form action="Addchuli.php" method="post">
    
        <div>
            代号:
            <input type="text" name="code" />
        </div>
        <div>
            姓名:
            <input type="text" name="name" />   
        </div>
        <div>
            性别:<input type="radio" value="false" name="sex" /><input type="radio" value="ture" name="sex" />   
        </div>
        <div>
            民族:                            <!--变成下拉列表-->
            <select name="nation">
            <?php
            //造链接对象
            $db =new MySQLi("localhost","root","123456","xinjian");
            //判断
            !mysqli_connect_error() or die("链接失败");
            //写sql语句
            $sql ="select * from Nation";
            //执行
            $result =$db->query($sql);
            //处理
            $attr=$result->fetch_all();
            for($i=0;$i<count($attr);$i++)
            {
                echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
                }
            
            ?>
            </select>  
        </div>
        <div>
            生日:
            <input type="text" name="birthday" />   
        </div>
        <div>
            <input type="submit" value="确定" />
            <a href="AM.php">返回主页面</a>
        </div>
    </form>
    
    </body>
    </html>

    三.建立一个Addchuli.php 用于处理Add.php里面传来的数据

    <?php
    
    $code =$_POST["code"];
    $name =$_POST["name"];
    $sex =$_POST["sex"];
    $nation =$_POST["nation"];
    $birthday =$_POST["birthday"];
    
    //
    $db = new MySQLi("localhost","root","123456","xinjian");
    
    !mysqli_connect_error() or die("链接失败");
    
    $sql= "insert into Info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')";
    
    $result =$db->query($sql);
    if($result)
    {
        header("location:Add.php");
        }else
        {
            echo"执行失败";
            }
    
    header("location:Add.php");

    四.建立一个Delete.php文件,点击“删除”跳转到本页面处理

    <?php
    
    $code =$_GET["code"];
    
            //造链接对象
            $db =new MySQLi("localhost","root","123456","xinjian");
            //判断
            !mysqli_connect_error() or die("链接失败");
            //写sql语句
            $sql ="delete from Info where Code ='{$code}'";
            //执行
            $result =$db->query($sql);
            
            if($result)
            {
                header("location:AM.php");
                }else
                {
                    echo "删除失败";
                    }

    五.建立一个Update.php文件,点击“修改”,跳转本页面,显示当前数据

    注意:性别和民族。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <?php
    $code = $_GET["code"];
            //造链接对象
            $db =new MySQLi("localhost","root","123456","xinjian");
            //判断
            !mysqli_connect_error() or die("链接失败");
            //写sql语句
            $sqlxq ="select * from Info where Code='{$code}'";        //改个名 不跟下面重复
            //执行
            $resultxq =$db->query($sqlxq);
            
            $attrxq = $resultxq->fetch_row();
    
    ?>
    
    <form action="Updatechuli.php" method="post">
    
        <div>
            代号:
            <input readonly="readonly" type="text" name="code" value="<?php echo $attrxq[0]; ?>" />
        </div>
        <div>
            姓名:
            <input type="text" name="name" value="<?php echo $attrxq[1];?>" />   
        </div>
        <div>
            性别:<input type="radio" value="true" name="sex"  <?php echo $attrxq[2]?"checked='checked'":"";  ?> /><input type="radio" value="false" name="sex"  <?php echo $attrxq[2]?"":"checked='checked'"; ?> />   
           
        </div>
        <div>
            民族:                            <!--变成下拉列表-->
            <select name="nation">
            <?php
            //造链接对象
            //$db =new MySQLi("localhost","root","123456","xinjian");  上面有了 可以删了
            //判断
            !mysqli_connect_error() or die("链接失败");
            //写sql语句
            $sql ="select * from Nation";                
            //执行
            $result =$db->query($sql);
            //处理
            $attr=$result->fetch_all();
            for($i=0;$i<count($attr);$i++)
            {
                if($attrxq[3]==$attr[$i][0])
                {
                    echo "<option  selected='selected' value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
                    }
                echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
                }
            
            ?>
            </select>  
        </div>
        <div>
            生日:
            <input type="text" name="birthday" value="<?php echo $attrxq[4];  ?>" />   
        </div>
        <div>
            <input type="submit" value="修改" />
            <a href="AM.php">返回主页面</a>
        </div>
    </form>
    
    </body>
    </html>

    六.建立一个Updatechuli.php文件,用来接收修改的信息

    <?php
    
    
    $code =$_POST["code"];
    $name =$_POST["name"];
    $sex =$_POST["sex"];
    $nation =$_POST["nation"];
    $birthday =$_POST["birthday"];
    //造链接
    $db = new MySQLi("localhost","root","123456","xinjian");
    //验证是否出错
    !mysqli_connect_error() or die("链接失败");
    //写sql语句
    $sql= "update info set name='{$name}',sex={$sex},nation ='{$nation}',birthday='{$birthday}' where code='{$code}'";
    
    //执行
    $result =$db->query($sql);
    
    if($result)
    {
        header("location:AM.php");
        
    }
    else
    {    
        echo"修改失败";
    }
  • 相关阅读:
    P站画师 GTZ taejune 精选4k插画壁纸
    点、向量与坐标系
    一些几何
    画直线算法 Line drawing algorithm
    DX11 学习大纲
    插值 Interpolation
    The History of Computer Graphics
    vue中的请求拦截响应
    Event loop
    小程序使用wx.navigateTo()跳转失败
  • 原文地址:https://www.cnblogs.com/zhanghaozhe8462/p/5319204.html
Copyright © 2011-2022 走看看