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
$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)装账

<?php
session_start();
echo "<h2>欢迎<font color='red'>".$_SESSION['user']."</font>登录成功<h2>";
$user=$_SESSION['user'];
try {
    $pdo= new PDO("mysql:host=127.0.0.1;dbname=web5","root","123456");
}catch (PDOException $error){
    print_r($error->getMessage());
}
$sql="select * from money where user = '$user'";
$res=$pdo->query($sql);
$data=$res->fetchAll(2);
?>
<!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="trade_do.php" method="post">
        <table class="table" border="1" style="width: 800px">
            <tr>
                <td>本人账号</td>
                <td><input type="text" name="usernum" value="<?php echo $data[0]['usernum'] ?>"></td>
            </tr>
            <tr>
                <td>可用余额</td>
                <td><input type="text" name="money" value="<?php echo $data[0]['money'] ?>"></td>
            </tr>
            <tr>
                <td>对方账户</td>
                <td><input type="text" name="usernum"></td>
            </tr>
            <tr>
                <td>转账金额</td>
                <td><input type="text" name="tomoney"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="转账" class="btn btn-primary"></td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>

(4)转账处理

<?php
session_start();
$user=$_SESSION['user'];
$usernum=$_POST['usernum'];
$money=$_POST['money'];
$touser=$_POST['touser'];
$tomoney=$_POST['tomoney'];

try {
    $pdo= new PDO("mysql:host=127.0.0.1;dbname=web5","root","123456");
}catch (PDOException $error){
    print_r($error->getMessage());
}
$pdo->beginTransaction();
$sql1="update money set money=money-'$tomoney' where user = '$user'";
$res1=$pdo->exec($sql1);

$sql2="update money set money=money+'$tomoney' where usernum = '$usernum'";
$res2=$pdo->exec($sql2);
if($tomoney>$money){
    $pdo->rollBack();
    echo "<script>alert('余额不足');location.href='trade.php';</script>";
}
if($res1 && $res2){
    $pdo->commit();
    echo "<script>alert('转账成功');location.href='trade.php';</script>";
}else{
    $pdo->rollBack();
    echo "<script>alert('转账失败');location.href='trade.php';</script>";
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值