用户信息添加、修改、删除
程序员文章站
2022-03-07 19:53:19
...
用户信息添加、修改、删除
添加用户信息 adduser.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>添加用户</title>
<style>
.form {
width: 360px;
margin: 0 auto;
padding: 10px;
}
form {
display: grid;
gap: 0.5rem;
}
fieldset {
display: grid;
gap: 0.5rem;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<div class="form">
<h2>添加用户</h2>
<form action="createuser.php" method="post">
<fieldset>
<legend>必填项</legend>
<div>
<label for="account">用名:</label>
<input id="account" type="text" name="account" required />
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required />
</div>
</fieldset>
<fieldset>
<legend>选填项</legend>
<div>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" />
</div>
<div>
<label for="email">邮箱:</label>
<input id="email" type="email" name="email" />
</div>
<div>
<label for="phone">电话:</label>
<input id="phone" type="number" name="phone" />
</div>
</fieldset>
<div>
<label for="phone">状态:</label>
<select name="state" id="">
<option value="1">开启</option>
<option value="2">关闭</option>
</select>
</div>
<button>添加</button>
</form>
</div>
</body>
</html>
数据库链接,创建PDO对象文件 linkdb.php
<?php
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=prodb','root','123456');
}catch(PDOException $e){
// 抛出错误,错误是你可以定义的
echo '数据库连接失败' . $e->getMessage();
}
添加用户信息后台文件createuser.php
<?php
require_once "linkdb.php";
if(!empty($_POST)){
$account=trim($_POST['account']);
$password=md5(trim($_POST['password']));
$name=trim($_POST['name'])??"";
$email=trim($_POST['email'])??"";
$phone=$_POST['phone'];
// var_dump(is_integer($phone),$phone);
$createTime=time();
$updateTime=time();
$state=$_POST['state'];
$pdo->query('SET NAMES utf8');
$sql="INSERT INTO `php_user` SET `account`=? ,`password`=?,";
$sql.="`name`=?,`email`=?,`phone`=?,`create_time`=?,`update_time`=?,`state`=?";
$pre=$pdo->prepare($sql);
$exec=$pre->execute(
[
$account,
$password,
$name,
$email,
$phone,
$createTime,
$updateTime,
$state
]
);
if($exec){
echo $pre -> rowCount();
echo '<hr>';
echo $pdo -> lastInsertId();
echo '<script>alert("添加成功");location.href="showuser.php"</script>';
}else{
print_r($pre->errorInfo());
}
}
显示用户信息文件 showuser.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示用户信息</title>
<style>
/* 1. 添加表格线,添加给单元格 */
tbody td,
th {
border: 1px solid;
}
/* 2. 折叠表格线 */
table {
border-collapse: collapse;
width: 90%;
/* 对齐 */
text-align: center;
margin: 2em auto;
}
/* 标题 */
table caption {
font-weight: bolder;
margin-bottom: 1em;
}
table thead {
background-color: lightgreen;
}
tbody tr:nth-of-type(2n){
background-color: #ddd;
}
</style>
</head>
<body>
<?php
require_once "linkdb.php";
$sql='SELECT * FROM `php_user`';
$pre=$pdo->prepare($sql);
$pre -> bindColumn('id',$id);
$pre -> bindColumn('account',$account);
$pre -> bindColumn('name',$name);
$pre -> bindColumn('phone',$phone);
$pre -> bindColumn('email',$email);
$pre -> bindColumn('create_time',$create_time);
$pre -> bindColumn('update_time',$update_time);
$pre -> bindColumn('state',$state);
$exec=$pre->execute();
?>
<div class="show">
<table>
<caption>用户信息</caption>
<thead>
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>ID</th>
<th>用户名</th>
<th>姓名</th>
<th>电话</th>
<th>邮箱</th>
<th>创建时间</th>
<th>最后登录时间</th>
<th>状态</th>
</tr>
</thead>
<tbody>
<?php while ($pre->fetch(PDO::FETCH_ASSOC)):?>
<tr>
<td><input type="checkbox" class="chk_item"></td>
<td class="id"><?=$id?></td>
<td><a href="edituser.html?id=<?=$id?>" title="点击修改用户信息" ><?=$account?></a></td>
<td><?=$name?></td>
<td><?=$phone?></td>
<td><?=$email?></td>
<td><?=date('Y-m-d',$create_time)?></td>
<td><?=date('Y-m-d',$update_time)?></td>
<td><?=$state==1?'开启':'关闭'?></td>
</tr>
<?php endwhile ?>
</tbody>
<tfoot>
<tr><td colspan="3"><button onclick="del()">删除</button></td>
<td><a href="adduser.html">添加用户</a></td>
</tr>
</tfoot>
</table>
</div>
<script>
const chkAll=document.querySelector("#check_all");
const chkItems=document.querySelectorAll(".chk_item");
chkAll.onchange=ev=>chkItems.forEach(item=>item.checked=ev.target.checked);
chkItems.forEach(item=>item.onchange=()=>chkAll.checked=[...chkItems].every(item=>item.checked));
function del(){
if(confirm('您确定要删除这些用户吗?')){
const chkItems=document.querySelectorAll(".chk_item");
chkItems.forEach(item=>{
if(item.checked){
const id=item.parentElement.nextElementSibling.textContent;
let isDel=delOpt(id);
if(isDel){
// console.log( item.parentElement.parentElement);
item.parentElement.parentElement.remove();
}
}
})
}else{
alert("NO");
}
}
async function delOpt(id){
const response = await fetch("del_user.php?id=" + id);
const comments = await response.json();
return comments;
}
</script>
</body>
</html>
- 选中,点击删除
删除用户文件 del_user.php
<?php
require_once "linkdb.php";
$id=$_GET['id'];
$sql="DELETE FROM `php_user` WHERE id=$id";
$pre=$pdo->prepare($sql);
$exec=$pre->execute();
if($exec){
echo 1;
}else{echo 0;}
修改用户信息 页面文件edituser.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>更新用户</title>
<style>
.form {
width: 360px;
margin: 0 auto;
padding: 10px;
}
form {
display: grid;
gap: 0.5rem;
}
fieldset {
display: grid;
gap: 0.5rem;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<div class="form">
<h2>更新用户信息</h2>
<form action="updateuser.php" method="post">
<fieldset>
<legend>用户信息</legend>
<div>
<input type="hidden" name="id" />
<label for="account">用名:</label>
<input id="account" type="text" name="account" required readonly />
</div>
<div>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" />
</div>
<div>
<label for="email">邮箱:</label>
<input id="email" type="email" name="email" />
</div>
<div>
<label for="phone">电话:</label>
<input id="phone" type="number" name="phone" />
</div>
</fieldset>
<div>
<label for="phone">状态:</label>
<select name="state" id="">
<option value="1">开启</option>
<option value="2">关闭</option>
</select>
</div>
<button>修改</button>
</form>
</div>
<script>
let url = location.search;
let id = url.substr(4);
if (id > 0) {
fetch("searchbyid.php?id=" + id)
.then((response) => response.json())
.then((json) => {
const form = document.forms[0];
form.id.value = json.id;
form.account.value = json.account;
form.account.readonly = true;
form.phone.value = json.phone;
form.email.value = json.email;
form.name.value = json.name;
form.state.value = json.state;
});
} else {
}
</script>
</body>
</html>
修改用户信息 后台文件updateuser.php
<?php
require_once "linkDB.php";
if(!empty($_POST)){
$id=$_POST['id'];
$account=trim($_POST['account']);
$name=trim($_POST['name'])??"";
$email=trim($_POST['email'])??"";
$phone=$_POST['phone'];
// var_dump(is_integer($phone),$phone);
$updateTime=time();
$state=$_POST['state'];
$pdo->query('SET NAMES utf8');
$sql="UPDATE `php_user` SET `name`=?,`email`=?,`phone`=?,`update_time`=?,`state`=? WHERE id=?";
$pre=$pdo->prepare($sql);
$exec=$pre->execute(
[
$name,
$email,
$phone,
$updateTime,
$state,
$id
]
);
if($exec){
echo $pre -> rowCount();
echo '<hr>';
echo $pdo -> lastInsertId();
echo '<script>alert("修改成功");location.href="showuser.php"</script>';
}else{
print_r($pre->errorInfo());
}
}
上一篇: 使用面向对象方法实现用户信息增删改查
下一篇: php自学需要多久?