查询一张表进行添加删除修改

主页面,点击删除,直接删除一条数据

点击添加数据,出现下面页面,点击确定数据添加到数据库

 

点击修改,出现下面页面,显示本条信息,并可以修改

代码

先建一个主页面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

<!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

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

 

posted @ 2016-05-04 16:55  梦里梦到梦  阅读(248)  评论(0编辑  收藏  举报