PDO用户模块开发
index.php
<?php
include 'config.php';
$sql="select * from user";
$smt=$pdo->prepare($sql);
$smt->execute();
$rows=$smt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户模块</title>
<link rel="stylesheet" href="/bs/css/bootstrap.css">
<script src='/bs/js/jquery.js'></script>
<script src='/bs/js/bootstrap.js'></script>
<style>
td,th{
text-align: center;
vertical-align:middle!important;
}
</style>
</head>
<body>
<div class="container">
<h1 class="page-header">
用户管理
</h1>
<table class='table table-striped table-hover table-bordered'>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>修改</th>
<th>删除</th>
</tr>
<?php
foreach($rows as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['username']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td><a href='edit.php?id={$row['id']}' class='btn btn-warning'>修改</a></td>";
echo "<td><a href='delete.php?id={$row['id']}' class='btn btn-danger'>删除</a></td>";
echo "</tr>";
}
?>
</table>
<p>
<a href="add.php" class='btn btn-primary btn-lg'>添加</a>
</p>
</div>
</body>
</html>
config.php
<?php
$servername = "localhost";
$username = "root";
$password = "123";
$dbname='myweb';
$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->exec('set names utf8');
?>
delete.php
<?php
include 'config.php';
$id=$_GET['id'];
$sql="delete from user where id={$id}";
$smt=$pdo->prepare($sql);
if($smt->execute()){
echo "<script>location='index.php'</script>";
}
?>
add.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户添加</title>
<link rel="stylesheet" href="/bs/css/bootstrap.css">
<script src='/bs/js/jquery.js'></script>
<script src='/bs/js/bootstrap.js'></script>
</head>
<body>
<div class="container">
<h1 class="page-header">
用户管理
</h1>
<form action="insert.php" method='post'>
<div class="form-group">
<label>用户名:</label>
<input type="text" class="form-control" name='username' placeholder='用户名'>
</div>
<div class="form-group">
<label>密码:</label>
<input type="text" class="form-control" name='password' placeholder='密码'>
</div>
<div class="form-group">
<input type="submit" value="添加" class='btn btn-primary btn-lg'>
</div>
</form>
</div>
</body>
</html>
insert.php
<?php
include 'config.php';
$username=$_POST['username'];
$password=$_POST['password'];
$sql="insert into user(username,age) values('{$username}','{$password}')";
$smt=$pdo->prepare($sql);
if($smt->execute()){
echo "<script>location='index.php'</script>";
}
?>
update.php
<?php
include 'config.php';
$id=$_POST['id'];
$username=$_POST['username'];
$password=$_POST['password'];
$sql="update user set username='{$username}',age='{$password}' where id={$id}";
$smt=$pdo->prepare($sql);
if($smt->execute()){
echo "<script>location='index.php'</script>";
}
?>
edit.php
<?php
include 'config.php';
$id=$_GET['id'];
$sql="select * from user where id={$id}";
$smt=$pdo->prepare($sql);
$smt->execute();
$row=$smt->fetch(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户添加</title>
<link rel="stylesheet" href="/bs/css/bootstrap.css">
<script src='/bs/js/jquery.js'></script>
<script src='/bs/js/bootstrap.js'></script>
</head>
<body>
<div class="container">
<h1 class="page-header">
用户管理
</h1>
<form action="update.php" method='post'>
<div class="form-group">
<label>用户名:</label>
<input type="text" class="form-control" name='username' placeholder='用户名' value='<?php echo $row['username'] ?>'>
</div>
<div class="form-group">
<label>密码:</label>
<input type="text" class="form-control" name='password' placeholder='密码' value='<?php echo $row['password'] ?>'>
</div>
<input type="hidden" name="id" value='<?php echo $row['id'] ?>'>
<div class="form-group">
<input type="submit" value="修改" class='btn btn-primary btn-lg'>
</div>
</form>
</div>
</body>
</html>