php实现简单的CRUD操作

<html>
<head>
<title>学生管理信息</title>
</head>

<body>
	<center>
         <?php include 'menu.php';?>
         <h3>信息浏览</h3>
		<table width="600" border="1">
			<tr>
				<td>ID</td>
				<td>姓名</td>
				<td>性别</td>
				<td>年龄</td>
				<td>班级</td>
				<td>操作</td>
			</tr>
			<?php 
			  //连接数据库
			    try {
			    	$pdo=new PDO("mysql:host=localhost;dbname=test;","root","562436");
			    } catch (Exception $e) {
                     die("数据库连接失败".$e->getMessage());
			    }
			    //执行SQL语句
			    $sql='select * from cms_stu';
			    foreach ($pdo->query($sql) as $row){
			    	echo "<tr>";
			    	echo "<td>{$row['id']}</td>";
			    	echo "<td>{$row['name']}</td>";
			    	
			    	if($row['sex']=='m'){
			    		echo "<td>男</td>";
			    	}else if($row['sex']=='w'){
			    		echo "<td>女</td>";
			    	}
			    	echo "<td>{$row['age']}</td>";
			    	echo "<td>{$row['classid']}班</td>";
			    	echo "<td><a href='javascript:doDel({$row['id']})'>删除</a> | <a href='edit.php?id={$row['id']}'>修改</a></td>";
			    	echo "</tr>";
			    	
			    }
			    
			   
		     ?>
		</table>
	</center>
</body>

<script type="text/javascript">
   function doDel(id){
	   if(confirm('是否确定删除?')){
               window.location='action.php?action=del&id='+id;
		   }
	   }
</script>
</html>




<html>
   <h2>学生信息管理</h2>
   <a href="index.php">浏览学生信息</a>
   <a href="add.php">增加学生信息</a>
</html>


<html>
<head>
<title>学生管理信息</title>
</head>

<body>
	<center>
         <?php include 'menu.php';?>
         <h3>增加学生</h3>
		<form action="action.php?action=add" method="post">
			<table>
				<tr>
					<td>姓名</td>
					<td><input type="text" name="name" /></td>
				</tr>
				<tr>
					<td>性别</td>
					<td>
					  <input type="radio" name="sex" value="m"/>男
					  <input type="radio" name="sex" value="w"/>女
					</td>
				</tr>
				<tr>
					<td>年龄</td>
					<td><input type="text" name="age" /></td>
				</tr>
				<tr>
					<td>班级</td>
					<td><input type="text" name="classid" /></td>
				</tr>
				<tr>
					<td>
					    <input type="submit" value="增加" />
					    <input type="reset" value="重置" />
					</td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>




<?php
try {
	$pdo=new PDO("mysql:host=localhost;dbname=test;","root","562436");
} catch (Exception $e) {
	die("数据库连接失败".$e->getMessage());
}

//操作
switch ($_GET['action']){
	case "add":
		  $name=$_POST['name'];
		  $sex=$_POST['sex'];
		  $age=$_POST['age'];
		  $classid=$_POST['classid'];
		  
		  $sql="insert into cms_stu values(null,'{$name}','{$sex}','{$age}','{$classid}')";
		  
		  $rw=$pdo->exec($sql);
		  if($rw>0){
		  	echo "<script>alert('增加成功');window.location='index.php'</script>";
		  }else{
		  	echo "<script>alert('增加失败');window.history.back()</script>";
		  }
		  break;
	case "del":
		$id=$_GET['id'];
		$sql="delete from cms_stu where id={$id}";
		$pdo->exec($sql);
		header("Location:index.php");
		break;
	case "edit":
		$name=$_POST['name'];
		$sex=$_POST['sex'];
		$age=$_POST['age'];
		$classid=$_POST['classid'];
		$id=$_POST['id'];

		$sql="update cms_stu set name='{$name}',sex='{$sex}',age='{$age}',classid='{$classid}' where id={$id}";
		 $rw=$pdo->exec($sql);
		  if($rw>0){
		  	echo "<script>alert('修改成功');window.location='index.php'</script>";
		  }else{
		  	echo "<script>alert('修改失败');window.history.back()</script>";
		  }
		  break;
		  
		  
}

\



<html>
<head>
<title>学生管理信息</title>
</head>

<body>
	<center>
         <?php include 'menu.php';
         try {
         	$pdo=new PDO("mysql:host=localhost;dbname=test;","root","562436");
         } catch (Exception $e) {
         	die("数据库连接失败".$e->getMessage());
         }
         
           $sql="select * from cms_stu where id=".$_GET['id'];
           $stmt=$pdo->query($sql);
           if($stmt->rowCount() >0){
           	    $stu=$stmt->fetch(PDO::FETCH_ASSOC);
           }else{
           	die("没有修改");
           }
         ?>
         <h3>修改学生</h3>
		<form action="action.php?action=edit" method="post">
			<table>
				<tr>
					<td>姓名</td>
					<td><input type="text" name="name" value="<?php echo $stu['name']?>"/></td>
				</tr>
				<tr>
					<td>性别</td>
					<td>
					  <input type="radio" name="sex" value="m" <?php echo $stu['sex']=="m" ? "checked":"" ?>/>男
					  <input type="radio" name="sex" value="w" <?php echo $stu['sex']=="w" ? "checked":"" ?>/>女
					</td>
				</tr>
				<tr>
					<td>年龄</td>
					<td><input type="text" name="age" value="<?php echo $stu['age']?>"/></td>
				</tr>
				<tr>
					<td>班级</td>
					<td><input type="text" name="classid" value="<?php echo $stu['classid']?>"/></td>
				</tr>
				<input type="hidden" name="id" value="<?php echo $stu['id']?>"/>
				<tr>
					<td>
					    <input type="submit" value="修改" />
					    <input type="reset" value="重置" />
					</td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>


CREATE TABLE `cms_stu` (
  `id` mediumint(6) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '',
  `sex` char(2) NOT NULL COMMENT '男',
  `age` int(11) NOT NULL,
  `classid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;



你可能感兴趣的:(html,PHP,mysql,javascrip)