PHP:PDO用户模块开发-用户管理-增、删、改、查

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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值