PHP PDO预处理的形式实现数据登录,添加,查询

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)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值