1.直接上干货,样式,起名规范,自行修改
(1)登录
<?php
session_start();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<center>
<form action="login_do.php" method="post">
<table class="table" border="1" style="width: 500px">
<tr>
<td>用户名</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录" class="btn-primary" ></td>
</tr>
</table>
</form>
</center>>
</body>
</html>
(2)登录处理+占位符处理
<?php
//连接mysql
//写sql语句
//执行sql语句
$user=$_POST['user'];
$password=$_POST['password'];
try {
$pdo=new PDO('mysql:host=127.0.0.1;dbname=web5;port=3306;charst=utf8','root','123456');
}catch(PDOException $error){
print_r($error->getMessage());
}
$sql="select * from login_user where user = '$user'";
$pdostatement=$pdo->query($sql);
$res=$pdostatement->fetchAll(PDO::FETCH_ASSOC);
if(!$res){
echo "用户名不存在";die;
}
$sql="select * from login_user where user = '$user'and password = '$password'";
$pdostatement=$pdo->query($sql);
$res=$pdostatement->fetchAll(PDO::FETCH_ASSOC);
if(!$res){
echo "密码不存在";die;
}
header("location:addform.php");
?>
//下面是占位符处理
<?php
$user=$_POST['user'];
$password=$_POST['password'];
try {
$pdo= new PDO("mysql:host=127.0.0.1;dbname=web5","root","123456");
}catch (PDOException $error){
print_r($error->getMessage());
}
if(empty($user)){
echo "用户名不能为空";die;
}
if(empty($password)){
echo "密码不能为空";die;
}
$sql="select * from login_user where user = :user and password = :password";
$obj=$pdo->prepare($sql);
$obj->bindParam(':user',$user);
$obj->bindParam(':password',$password);
$res=$obj->execute();
if($res){
$result=$obj->rowCount();
if ($result){
echo "登录成功";
}else{
echo "登录失败";die;
}
}
session_start();
$user=$_SESSION['user'];
header("location:trade.php");
?>
(3)添加
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<center>
<form action="add_do.php" method="post" enctype="multipart/form-data">
<table class="table" border="1" style="width: 500px">
<tr>
<td>标题</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>作者</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>分类</td>
<td><input type="text" name="type"></td>
</tr>
<!-- <tr>-->
<!-- <td>图片</td>-->
<!-- <td><input type="file" name="img"></td>-->
<!-- </tr>-->
<tr>
<td>简介</td>
<td><input type="text" name="talk"></td>
</tr>
<tr>
<td>浏览次数</td>
<td><input type="text" name="num"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="添加" class="btn btn-primary"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
(4)添加处理
<?php
$title=$_POST['title'];
$author=$_POST['author'];
$type=$_POST['type'];
$img=$_FILES['img'];
$talk=$_POST['talk'];
$num=$_POST['num'];
//var_dump($num);die;
try {
$pdo=new PDO('mysql:host=127.0.0.1;dbname=web5;port=3306;charst=utf8','root','123456');
}catch(PDOException $error){
print_r($error->getMessage());
}
$sql="insert into author values (null,'$title','$author','$type',$num,'$talk')";
$res=$pdo->exec($sql);
if($res){
echo "<script>alert('添加成功');location.href='showform.php';</script>";
}else{
echo "<script>alert('添加失败');location.href='addform.php';</script>";
}
?>
(5)展示
<?php
$page=isset($_GET['page'])?$_GET['page']:1;
$list=3;
$start=($page-1)*$list;//计算偏移量
$pdo=new pdo("mysql:host=127.0.0.1;dbname=web5;",'root','123456');
$sql="select * from author limit $start,$list";
$res=$pdo->query($sql);
$arr=$res->fetchAll(PDO::FETCH_ASSOC);
echo $res->rowCount();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<center>
<table class="table" border="1">
<tr>
<td>编号</td>
<td>标题</td>
<td>作者</td>
<td>分类</td>
<td>简介</td>
<td>浏览次数</td>
</tr>
<?php foreach ($arr as $k=>$v) {?>
<tr>
<td><?php echo $v['id'] ?></td>
<td><?php echo $v['title'] ?></td>
<td><?php echo $v['author'] ?></td>
<td><?php echo $v['type'] ?></td>
<td><?php echo $v['talk'] ?></td>
<td><?php echo $v['num'] ?></td>
</tr>
<?php } ?>
</table>
</center>
</body>
</html>
<?php
$sql="select * from author";
$obj=$pdo->query($sql);
$sum=$obj->rowCount();
$pages=ceil($sum/$list);
$prev=$page>1?$page-1: 1;
$next=$page<$pages?$page+1:$pages;
echo "<a href='showform.php?page=1'>".'首页'."</a> "; // 第一页
echo "<a href='showform.php?page=<?php echo $prev ?>'>".'上一页'."</a> ";
for ($i=1; $i<=$pages; $i++) {
echo "<a href='showform.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='showform.php?page=<?php echo $next ?>'>".'下一页'."</a> ";
echo "<a href='showform.php?page=$total_pages'>".'尾页'."</a> "; // 最后一页
?>
(6)