student

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


	require_once("head.html");
	require_once("dbop.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


	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


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

del.php


	require_once("dbop.php");
?>

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

show.php


	//查询并显示所有学生的信息
	require_once("head.html");
	require_once("dbop.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>
	
		while($ro=mysqli_fetch_array($ans)){
	?>	
	<tr>
		<td> echo $ro['id'];?></td>
		<td> echo $ro['stuid'];?></td>
		<td> echo $ro['name'];?></td>
		<td> echo $ro['sex'];?></td>
		<td> echo $ro['class'];?></td>
		<td> echo $ro['tel'];?></td>
		<td><a href="del.php?id=$ro['id']; ?>" style="color: #1C86EE;">删除</a>
			<a href="update.php?id=$ro['id']; ?>" style="color: #1C86EE;">修改</a></td>
	</tr>
	 } ?>
</table>

up.php


	require_once("dbop.php");
	$id=$_GET['id'];
	$stuid=$_POST['stuid'];
	$name=$_POST['name'];
	$sex=$_POST['sex'];
	$class=$_POST['class'];
	$tel=$_POST['tel'];
?>

	$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


	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=$id;?> " method="post">
	<label>数据库id</label>
	<input type="text" name="id" id="id" value="$id;?>" disabled="disabled" /><br />
	<label>学号</label>
	<input type="text" name="stuid" id="" value="$ro['stuid'];?>"/><br />
	<label>姓名</label>
	<input type="text" name="name" id="name" value="$ro['name'];?>" /><br />
	<label>性别</label>
	<input type="text" name="sex" id="" value="$ro['sex'];?>"/><br />
	<label>班级</label>
	<input type="text" name="class" id="" value="$ro['class'];?>"/><br />
	<label>电话</label>
	<input type="text" name="tel" id="" value="$ro['tel'];?>"/><br />
	<input type="submit" value="修改" />
</form>
 }?>

你可能感兴趣的:(php)