student

    技术2022-07-10  119

    head.html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <ul> <li><a href="add.php">增加</a></li> <li><a href="show.php">删除</a></li> <li><a href="show.php">修改</a></li> <li><a href="show.php">查询</a></li> </ul> </body> </html>

    add.php

    <?php require_once("head.html"); require_once("dbop.php"); ?> <?php if(isset($_GET['stuid'])){ require('check.php'); $vail = check(); if($vail){ $sql="INSERT INTO student (`stuid`,`name`,`sex`,`class`,`tel`)". "VALUES('{$_GET['stuid']}','{$_GET['name']}','{$_GET['sex']}','{$_GET['class']}','{$_GET['tel']}')"; if(query($sql)==true){ echo "增加成功"; }else{ echo "增加失败"; } }else{ echo "格式错误"; } } ?> <form action="add.php" method="get"> <label>学号</label> <input type="text" name="stuid" id="" /><br /> <label>姓名</label> <input type="text" name="name" id="name" value="" /><br /> <label>性别</label> <input type="text" name="sex" id="" /><br /> <label>班级</label> <input type="text" name="class" id="" /><br /> <label>电话</label> <input type="text" name="tel" id="" /><br /> <input type="submit" value="新增加" /> </form>

    check.php

    <?php function check(){ $arr = str_split($_GET['tel']); foreach($arr as $a){ if($a<'0'||$a>'9'){ return false; } } $sql = "select count(stuid) from student where stuid='{$_GET['stuid']}' "; $ans=query($sql); $cnts=$ans->fetch_array(); if($cnts[0]!=0){ return false; } return true; } ?>

    dbop.php

    <?php function query($sql){ $db=new mysqli("localhost","root","","dome"); $db->query("set names utf8"); $ret = $db->query($sql); $db->close(); return $ret; } ?>

    del.php

    <?php require_once("dbop.php"); ?> <?php $did = $_GET['id']; echo $did; $sql="delete from `student` where `id`='$did' "; echo $sql; query($sql); header("Location:show.php"); ?>

    show.php

    <?php //查询并显示所有学生的信息 require_once("head.html"); require_once("dbop.php"); ?> <?php $sql="select * from student"; $ans=query($sql); ?> <table border="1px solid black "> <tr> <th>数据库id</th> <th>学号</th> <th>姓名</th> <th>性别</th> <th>班级</th> <th>电话</th> <th>功能</th> </tr> <?php while($ro=mysqli_fetch_array($ans)){ ?> <tr> <td><?php echo $ro['id'];?></td> <td><?php echo $ro['stuid'];?></td> <td><?php echo $ro['name'];?></td> <td><?php echo $ro['sex'];?></td> <td><?php echo $ro['class'];?></td> <td><?php echo $ro['tel'];?></td> <td><a href="del.php?id=<?php echo $ro['id']; ?>" style="color: #1C86EE;">删除</a> <a href="update.php?id=<?php echo $ro['id']; ?>" style="color: #1C86EE;">修改</a></td> </tr> <?php } ?> </table>

    up.php

    <?php require_once("dbop.php"); $id=$_GET['id']; $stuid=$_POST['stuid']; $name=$_POST['name']; $sex=$_POST['sex']; $class=$_POST['class']; $tel=$_POST['tel']; ?> <?php $sql="update student set stuid='".$stuid."',name='".$name."',sex='".$sex."',class='".$class."',tel='".$tel. "' WHERE id='".$id."'"; // echo $sql; if(query($sql)){ echo "修改成功"; }else{ echo "修改失败"; } header("Location:show.php"); ?>

    update.php

    <?php require_once("head.html"); require_once("dbop.php"); require('check.php'); $id=$_GET['id']; $sql="SELECT * FROM student WHERE id='".$id."'"; $ans=query($sql); if($ro=mysqli_fetch_array($ans)){ ?> <form action="up.php?id=<?php echo $id;?> " method="post"> <label>数据库id</label> <input type="text" name="id" id="id" value="<?php echo $id;?>" disabled="disabled" /><br /> <label>学号</label> <input type="text" name="stuid" id="" value="<?php echo $ro['stuid'];?>"/><br /> <label>姓名</label> <input type="text" name="name" id="name" value="<?php echo $ro['name'];?>" /><br /> <label>性别</label> <input type="text" name="sex" id="" value="<?php echo $ro['sex'];?>"/><br /> <label>班级</label> <input type="text" name="class" id="" value="<?php echo $ro['class'];?>"/><br /> <label>电话</label> <input type="text" name="tel" id="" value="<?php echo $ro['tel'];?>"/><br /> <input type="submit" value="修改" /> </form> <?php }?>
    Processed: 0.012, SQL: 9