zoukankan      html  css  js  c++  java
  • PHP访问数据,增删改

    主页面

    <h1>主页面</h1>
    
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>民族</td>
        <td>生日</td>
        <td>操作</td>
    </tr>
    
    <?php
    //1.造连接对象
    $db = new MySQLi("localhost","root","123","mydb");
    //2.判断是否出错
    !mysqli_connect_error() or die("连接失败!");
    //3.执行SQL语句
    $result = $db->query("select * from Info");
    //4.读取数据
    $attr = $result->fetch_all();
    
    foreach($attr as $v)
    {
        $sex = $v[2]?'男':'女';
        //处理民族名称
        $sqln = "select Name from Nation where Code='{$v[3]}'";
        $rnation = $db->query($sqln);
        $attrn = $rnation->fetch_assoc();
        
        echo "<tr>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$sex}</td>
        <td>{$attrn['Name']}</td>
        <td>{$v[4]}</td>
        <td>
            <a href='Delete.php?code={$v[0]}'>删除</a>
            <a href='Update.php?code={$v[0]}'>修改</a>
        </td>
        </tr>";
    }
    
    
    ?>

    增添数据

    add.php

    <h1>添加数据</h1>
    <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="男" name="sex" /><input type="radio" value="女" name="sex" /></div>
        <div>民族:
            <select name="nation">
                <?php
                
                $db = new MySQLi("localhost","root","123","mydb");
                $sql = "select * from Nation";
                $r = $db->query($sql);
                $attr = $r->fetch_all();
                foreach($attr as $v)
                {
                    echo "<option value='{$v[0]}'>{$v[1]}</option>";
                }
                
                ?>
            </select>
        </div>
        <div>生日:<input type="text" name="birthday" /></div>
        <div><input type="submit" value="添加数据" /></div>
    </form>
    <div><a href="main.php">主页面</a></div>

    addchuli.php

    <?php
    
    $code = $_POST["code"];
    $name = $_POST["name"];
    $sex = $_POST["sex"];
    $s = 1;
    if($sex == "女")
    {
        $s = 0;
    }
    
    $nation = $_POST["nation"];
    $birthday = $_POST["birthday"];
    
    $db = new MySQLi("localhost","root","123","mydb");
    $sql = "insert into Info values('{$code}','{$name}',{$s},'{$nation}','{$birthday}')";
    
    $result = $db->query($sql);
    
    if($result)
    {
        header("location:Add.php");//跳转到Add.php
    }
    else
    {
        echo "添加失败!";
    }

    删除数据

    delete.php

    <?php
    
    $code = $_GET["code"];
    
    $db = new MySQLi("localhost","root","123","mydb");
    $sql = "delete from Info where Code = '{$code}'";
    $r = $db->query($sql);
    if($r)
    {
        header("location:main.php");
    }
    else
    {
        echo "删除失败!";
    }

    修改主页

    update.php

    <body>
    <h1>修改数据</h1>
    
    <?php
    $code = $_GET["code"];
    $db = new MySQLi("localhost","root","123","mydb");
    $sinfo = "select * from Info where Code='{$code}'";
    $r = $db->query($sinfo);
    $arr = $r->fetch_row(); //这个人的所有信息
    ?>
    
    <form action="UpdateChuLi.php" method="post">
    
        <div><input type="hidden" name="code" value="<?php echo $arr[0] ?>" /></div>
        <div>姓名:<input type="text" name="name" value="<?php echo $arr[1] ?>" /></div>
        <div>性别:
            <input type="radio" value="男" name="sex" <?php echo $arr[2]?"checked='checked'":"" ?> /><input type="radio" value="女" name="sex" <?php echo $arr[2]?"":"checked='checked'" ?> /></div>
        <div>民族:
            <select name="nation">
                <?php
                
                $sql = "select * from Nation";
                $r = $db->query($sql);
                $attr = $r->fetch_all();
                foreach($attr as $v)
                {
                    if($v[0] == $arr[3])
                    {
                        echo "<option selected='selected' value='{$v[0]}'>{$v[1]}</option>";
                    }
                    else
                    {
                        echo "<option value='{$v[0]}'>{$v[1]}</option>";
                    }
                }
                
                ?>
            </select>
        </div>
        <div>生日:<input type="text" name="birthday" value="<?php echo $arr[4] ?>" /></div>
        <div><input type="submit" value="修改数据" /></div>
    </form>
    <div><a href="main.php">主页面</a></div>
    </body>

     updatechuli.php

    <?php
    
    $code = $_POST["code"];
    $name = $_POST["name"];
    $sex = $_POST["sex"];
    $s = 1;
    if($sex == "女")
    {
        $s = 0;
    }
    
    $nation = $_POST["nation"];
    $birthday = $_POST["birthday"];
    
    $db = new MySQLi("localhost","root","123","mydb");
    $sql="Update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}' ";
    
    $r = $db->query($sql);
    
    if($r)
    {
        header("location:main.php");
    }
    else
    {
        echo "修改失败!";
    }
  • 相关阅读:
    python os.path.dirname()
    python os.path.basename()方法
    python mmap对象
    python 读取二进制数据到可变缓冲区中
    sklearn常见分类器的效果比较
    用matplotlib获取雅虎股票数据并作图
    使用 lxml 中的 xpath 高效提取文本与标签属性值
    如何用 Python 爬取需要登录的网站
    python 线程及线程池
    使用Python代码处理Excel
  • 原文地址:https://www.cnblogs.com/wanlibingfeng/p/5464110.html
Copyright © 2011-2022 走看看