php實現簡單的CRUD操作
阿新 • • 發佈:2019-02-14
<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;