• 数据库操作


    主页面

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9 
    10 <h1>主页面</h1>
    11 <div style="100%">
    12 
    13 <?php
    14     
    15     //1.造连接对象
    16     $db = new mysqli("localhost","root","123","mydb");
    17     
    18     //2.判断是否连接成功
    19     if(mysqli_connect_error())
    20     {    
    21         echo "连接失败";
    22         
    23         //退出整个程序
    24         exit;
    25     }
    26     else
    27     {
    28         
    29         //3.写SQL语句
    30         $sql = "select * from Info";
    31         
    32         //4.执行SQL语句
    33         $result = $db->query($sql);
    34         
    35         //5.处理数据(提取数据)
    36         
    37         echo "<table width='100%' cellpadding='0' cellspacing='0' border='1'>";
    38         
    39         echo "<tr bgcolor='#66FF99'><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td><td>操作</td></tr>";
    40         
    41         //遍历每一条数据
    42         while($row = $result->fetch_row())
    43         {
    44             //处理性别
    45             $sex = (bool)$row[2]?"男":"女";
    46             
    47             //处理民族
    48             $nation = NationName($db,$row[3]);
    49             
    50             //处理生日        
    51             $birthday = date("Y年m月d日",strtotime($row[4]));
    52             
    53             echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$sex}</td><td>{$nation}</td><td>{$birthday}</td><td><a href='Delete.php?code=".$row[0]."'>删除</a>&nbsp;<a href='XiuGai.php?code=".$row[0]."'>修改</a></td></tr>";
    54         }
    55         
    56         echo "</table>";
    57         
    58     }
    59     
    60     
    61     
    62     
    63     //根据民族代号查询名族名称
    64     function NationName($db,$code)
    65     {
    66         //写SQL语句
    67         $sql = "select * from Nation where Code = '{$code}'";
    68         
    69         //执行SQL语句
    70         $result = $db->query($sql);
    71         
    72         //处理数据
    73         if($row = $result->fetch_row())
    74         {
    75             return $row[1];
    76         }
    77         else
    78         {
    79             return "";
    80         }
    81         
    82         
    83     }
    84     
    85 ?>
    86 </div>
    87 <div><a href="Add.php">添加数据</a></div>
    88 
    89 </body>
    90 </html>

    添加页面

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9 
    10 <h1>添加页面</h1>
    11 
    12 <form action="Insert.php" method="post">
    13 
    14 <div><span style="80px;">代号:</span><input type="text" name="code" /></div>
    15 
    16 <div><span style="80px;">姓名:</span><input type="text" name="name" /></div>
    17 
    18 <div><span style="80px;">性别:</span><input type="radio" name="sex" value="true" checked="checked" />男&nbsp;&nbsp;<input type="radio" name="sex" value="false"/>女</div>
    19 
    20 <div><span style="80px;">民族:</span>
    21 <select name="nation">
    22     <?php
    23         //1.造连接对象
    24     $db = new mysqli("localhost","root","123","mydb");
    25     
    26     //2.判断是否连接成功
    27     if(mysqli_connect_error())
    28     {    
    29         echo "连接失败";
    30         
    31         //退出整个程序
    32         exit;
    33     }
    34     else
    35     {
    36         //3.写SQL语句
    37         $sql = "select * from Nation";
    38         
    39         //4.执行SQL语句
    40         $result = $db->query($sql);
    41         
    42         //5.处理数据
    43         //遍历每一条数据
    44         while($row = $result->fetch_row())
    45         {            
    46             echo "<option value='{$row[0]}'>{$row[1]}</option>";
    47         }
    48     }
    49     ?>
    50 </select>
    51 </div>
    52 
    53 <div><span style="80px;">生日:</span><input type="text" name="birthday" /></div>
    54 
    55 <div><input type="submit" value="添加"/> &nbsp;&nbsp;<a href="Test.php">返回</a></div>
    56 
    57 
    58 </form>
    59 
    60 </body>
    61 </html>

    添加处理

     1 <?php
     2     
     3     $code = $_POST["code"];
     4     $name = $_POST["name"];
     5     $sex = $_POST["sex"];
     6     $nation = $_POST["nation"];
     7     $birthday = $_POST["birthday"];
     8     
     9     //1.造连接对象
    10     $db = new mysqli("localhost","root","123","mydb");
    11     
    12     //2.判断是否连接成功
    13     if(mysqli_connect_error())
    14     {    
    15         echo "连接失败";
    16         
    17         //退出整个程序
    18         exit;
    19     }
    20     else
    21     {
    22         //3.写SQL语句
    23         $sql = "insert into Info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')";
    24         
    25         
    26         //4.执行SQL语句
    27         $result = $db->query($sql);
    28         
    29         //判断是否添加成功
    30         
    31         if($result)
    32         {
    33             header("Location:Add.php");
    34         }
    35         else
    36         {
    37             echo "添加失败";
    38         }
    39     }
    40     
    41 ?>

    修改页面

      1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      2 <html xmlns="http://www.w3.org/1999/xhtml">
      3 <head>
      4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      5 <title>无标题文档</title>
      6 </head>
      7 
      8 <body>
      9 
     10 <h1>修改页面</h1>
     11 
     12 <?php
     13 
     14     $code = $_GET["code"];
     15     
     16     //1.造连接对象
     17     $db = new mysqli("localhost","root","123","mydb");
     18     
     19     //2.判断是否连接成功
     20     if(mysqli_connect_error())
     21     {    
     22         echo "连接失败";
     23         
     24         //退出整个程序
     25         exit;
     26     }
     27     else
     28     {
     29         //3.写SQL语句
     30         $sql = "select * from Info where Code='".$code."'";
     31         
     32         //4.执行SQL语句
     33         $result = $db->query($sql);
     34         
     35         //5.处理数据
     36         $row = $result->fetch_row();
     37 
     38     }
     39     
     40 ?>
     41 
     42 <form action="Update.php" method="post">
     43 
     44 <div><span style="80px;">代号:</span><input type="text" name="code" value="<?php echo $row[0] ?>" readonly="readonly" /></div>
     45 
     46 <div><span style="80px;">姓名:</span><input type="text" name="name" value="<?php echo $row[1] ?>" /></div>
     47 
     48 <div>
     49 <span style="80px;">性别:</span>
     50 <input type="radio" name="sex" value="true" <?php echo (bool)$row[2]?"checked='checked'":"" ?> />男&nbsp;&nbsp;
     51 <input type="radio" name="sex" value="false" <?php echo !(bool)$row[2]?"checked='checked'":"" ?>/> 52 </div>
     53 
     54 <div><span style="80px;">民族:</span>
     55 <select name="nation">
     56     <?php
     57         //1.造连接对象
     58     $db = new mysqli("localhost","root","123","mydb");
     59     
     60     //2.判断是否连接成功
     61     if(mysqli_connect_error())
     62     {    
     63         echo "连接失败";
     64         
     65         //退出整个程序
     66         exit;
     67     }
     68     else
     69     {
     70         //3.写SQL语句
     71         $sql = "select * from Nation";
     72         
     73         //4.执行SQL语句
     74         $result = $db->query($sql);
     75         
     76         //5.处理数据
     77         //遍历每一条数据
     78         while($rownation = $result->fetch_row())
     79         {        
     80             if($rownation[0] == $row[3])
     81             {
     82                 echo "<option selected='selected' value='{$rownation[0]}'>{$rownation[1]}</option>";
     83             }
     84             else
     85             {
     86                 echo "<option value='{$rownation[0]}'>{$rownation[1]}</option>";
     87             }
     88             
     89         }
     90     }
     91     ?>
     92 </select>
     93 </div>
     94 
     95 <div><span style="80px;">生日:</span><input type="text" name="birthday" value="<?php echo $row[4] ?>" /></div>
     96 
     97 <div><input type="submit" value="修改"/> &nbsp;&nbsp;<a href="Test.php">返回</a></div>
     98 
     99 
    100 </form>
    101 
    102 </body>
    103 </html>

    修改处理

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9 <?php
    10 
    11     $code = $_POST["code"];
    12     $name = $_POST["name"];
    13     $sex = $_POST["sex"];
    14     $nation = $_POST["nation"];
    15     $birthday = $_POST["birthday"];
    16     
    17     //1.造连接对象
    18     $db = new mysqli("localhost","root","123","mydb");
    19     
    20     //2.判断是否连接成功
    21     if(mysqli_connect_error())
    22     {    
    23         echo "连接失败";
    24         
    25         //退出整个程序
    26         exit;
    27     }
    28     else
    29     {
    30         //3.写SQL语句
    31         $sql = "update Info set Name='".$name."',Sex={$sex}, Nation='".$nation."',Birthday='".$birthday."' where Code = '".$code."'";
    32     
    33         //4.执行SQL语句
    34         $result = $db->query($sql);
    35         
    36         //判断是否修改成功
    37         
    38         if($result)
    39         {
    40             header("Location:Test.php");
    41         }
    42         else
    43         {
    44             echo "修改失败";
    45         }
    46     }
    47 
    48 ?>
    49 </body>
    50 </html>

    删除页面

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9 <?php
    10     
    11     $code = $_GET["code"];
    12     
    13     //1.造连接对象
    14     $db = new mysqli("localhost","root","123","mydb");
    15     
    16     //2.判断是否连接成功
    17     if(mysqli_connect_error())
    18     {    
    19         echo "连接失败";
    20         
    21         //退出整个程序
    22         exit;
    23     }
    24     else
    25     {
    26         //3.写SQL语句
    27         $sql = "delete from Info where Code = '".$code."'";
    28         
    29         //4.执行SQL语句
    30         $result = $db->query($sql);
    31         
    32         //判断是否删除成功
    33         if($result)
    34         {
    35             header("Location:Test.php");
    36         }
    37         else
    38         {
    39             echo "删除失败!";
    40         }
    41     }
    42     
    43 ?>
    44 </body>
    45 </html>
  • 相关阅读:
    linux 修改文件夹颜色 终端颜色
    每日更新FadeTop背景为必应图片
    odoo 去除动作菜单的删除按钮
    crontab详解
    odoo 创建初始数据库 切换当前数据库
    python for else
    lfi phpinfo
    python __dict__
    iscsi 开机自动挂载
    HP SSD smart path
  • 原文地址:https://www.cnblogs.com/sihuiming/p/5205187.html
Copyright © 2020-2023  润新知