• 数据的增删改


    以多个人的信息进行添加、删除、修改为例

    1.主页面

    <!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>
    <h1>主页面</h1>
    <table border="1" cellpadding="0" cellspacing="0" width="700px">
    <tr>
        <td>代号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>民族</td>
        <td>生日</td>
        <td>操作</td>
    </tr>
    <?php
        $db=new MySQLi("localhost","root","","mydb");
    	!mysqli_connect_error() or die("连接失败");
    	$sql="select * from Info";
    	$result=$db->query($sql);
    	$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>";
    	}
    ?>
    </table>
    <div><a href="add.php" target="_blank">添加数据</a></div>
    
    
    </body>
    </html>
    

      

    2.添加页面

    <!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>
    <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","","mydb");
    				 $sql="select * from nation";
    				 $result=$db->query($sql);
    				 $attr=$result->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" target="_blank">主页面</a></div>
    </body>
    </html>
    

      添加页面的处理代码

    <?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","","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 "添加失败!";
    	}
    

      

    3.修改页面

    <!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>
    <h1>修改数据</h1>
    
    <?php
    $code=$_GET["code"];
    $db=new MySQLi("localhost","root","","mydb");
    $sinfo="select * from info where code='{$code}'";
    $r=$db->query($sinfo);
    $arr=$r->fetch_row();    //这个人所有信息
    
    //var_dump($arr);
    
    ?>
    
    
    <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";
    				 $result=$db->query($sql);
    				 $attr=$result->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" target="_blank">主页面</a></div>
    </body>
    </html>
    

    修改页面的处理代码

    <?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","","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 "修改失败!";
    }
    

      

    4.删除数据的代码

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

      

  • 相关阅读:
    Oracle.EntityFrameworkCore使用时报错:Specified cast is not valid
    .net core webapi通过中间件获取请求和响应内容
    金额数字语音播报
    FluentData微型ORM
    记阿里巴巴数据采集
    给定一个N阶矩阵A,输出A的M次幂(M是非负整数)(Java)
    求出区间[a,b]中所有整数的质因数分解。(Java)(转载)
    最大公约数 最小公倍数(Java)
    十六进制转八进制(Java)
    杨辉三角形(java)
  • 原文地址:https://www.cnblogs.com/zst062102/p/5469375.html
Copyright © 2020-2023  润新知