将金额从一个用户帐户转移到另一个用户帐户

时间:2019-11-11 13:31:16

标签: php mysql ajax

我有一个数据库,其中包含诸如useraccountiduseridbalancetotalwithdrawalcreationdate之类的用户数据。我已经完成了结构和代码,但一直没有给我任何答复,并且发件人帐户上的资金没有改变。

下面是处理交易的HTML表单

<div id="make-trans" class="col-xs-12">
    <!-- PAGE CONTENT BEGINS-->                     
    <div id="w2w" class="container jumbotron">
        <h3 class="card-title center jumbotron1" style="background-color: #f88f20; color: #fff;"><i class="ace-icon fa fa-credit-card"></i> Wallet To Wallet</h3>
        <form class="form-horizontal" action="" role="form" novalidate>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-9">
                    <input type="text" name="userid" class="form-control" id="fname" placeholder="Agent Account ID">
                </div>
            </div>


            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-9">                                  
                    <input type='number' name='balance' class="form-control" id="sname" placeholder="Amount">
                </div>
            </div>


            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-9 center">
                    <a href="wallet-to-wallet.php" type="submit" name="submit" class="outline-btn info">Transfer</a>
                </div>
            </div>
        </form>              
    </div>
</div>

下面是处理交易的php代码

class TransactionDemo {

    const DB_HOST = 'localhost';
    const DB_NAME = 'trans';
    const DB_USER = 'root';
    const DB_PASSWORD = '';


    public function transfer($from, $to, $amount) {

        try {
            $this->pdo->beginTransaction();

            // get available amount of the transferer account
            $sql = 'SELECT balance FROM useraccount WHERE userid=:from';
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute(array(":from" => $from));
            $availableAmount = (int) $stmt->fetchColumn();
            $stmt->closeCursor();

            if ($availableAmount < $amount) {
                echo 'No money in your account';
                return false;
            }
            // deduct from the transferred account
            $sql_update_from = 'UPDATE useraccount
                SET balance = balance - :balance
                WHERE userid = :from';
            $stmt = $this->pdo->prepare($sql_update_from);
            $stmt->execute(array(":from" => $from, ":balance" => $amount));
            $stmt->closeCursor();

            // add to the receiving account
            $sql_update_to = 'UPDATE useraccount
                                SET balance = balance + :balance
                                WHERE userid = :to';
            $stmt = $this->pdo->prepare($sql_update_to);
            $stmt->execute(array(":to" => $to, ":balance" => $balance));

            // commit the transaction
            $this->pdo->commit();

            echo 'The amount has been transferred successfully';

            return true;
        } catch (PDOException $e) {
            $this->pdo->rollBack();
            die($e->getMessage());
        }
    }

    /**
     * Open the database connection
     */
    public function __construct() {
        // open database connection
        $conStr = sprintf("mysql:host=%s;dbname=%s", self::DB_HOST, self::DB_NAME);
        try {
            $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD);
        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }

    /**
     * close the database connection
     */
    public function __destruct() {
        // close the database connection
        $this->pdo = null;
    }

}

?>

现在我创建了一个transaction.php,并以action =“”的形式喜欢它,但是我没有收到任何错误,并且资金也没有动。

我在做什么错了?

2 个答案:

答案 0 :(得分:0)

您在此href中添加了 wallet-to-wallet.php

<div class="form-group">
            <div class="col-sm-offset-2 col-sm-9 center">
                <a href="wallet-to-wallet.php" type="submit" name="submit" class="outline-btn info">Transfer</a>
            </div>
        </div>

删除此代码或从 transaction.php 中移动代码,然后粘贴到 wallet-to-wallet.php 页面中。

答案 1 :(得分:-1)

 * PHP MySQL Transaction Demo
 */
class TransactionDemo {

    const DB_HOST = 'localhost';
    const DB_NAME = 'transferfund';
    const DB_USER = 'root';
    const DB_PASSWORD = '';

    /**
     * Open the database connection
     */
    public function __construct() {
        // open database connection
        $conStr = sprintf("mysql:host=%s;dbname=%s", self::DB_HOST, self::DB_NAME);
        try {
            $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD);
        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }

    /**
     * PDO instance
     * @var PDO 
     */
    private $pdo = null;

    /**
     * Transfer money between two accounts
     * @param int $from
     * @param int $to
     * @param float $amount
     * @return true on success or false on failure.
     */
    public function transfer($from, $to, $balance) {

        try {
            $this->pdo->beginTransaction();

            // get available amount of the transferer account
            $sql = 'SELECT balance FROM useraccount WHERE userid=:from';
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute(array(":from" => $from));
            $availableAmount = (int) $stmt->fetchColumn();
            $stmt->closeCursor();

            if ($availableAmount < $balance) {
                echo 'Insufficient amount to transfer';
                return false;
            }
            // deduct from the transferred account
            $sql_update_from = 'UPDATE useraccount
                SET balance = balance - :balance
                WHERE userid = :from';
            $stmt = $this->pdo->prepare($sql_update_from);
            $stmt->execute(array(":from" => $from, ":balance" => $balance));
            $stmt->closeCursor();


            // add to the receiving account
            $sql_update_to = 'UPDATE useraccount
                                SET balance = balance + :balance
                                WHERE userid = :to';
            $stmt = $this->pdo->prepare($sql_update_to);
            $stmt->execute(array(":to" => $to, ":balance" => $balance));

            // commit the transaction


                if ($this->pdo->commit()) {

                    header("Location: ../wallet2wallet_success.php");


                }else{

                    header("Location: ../wallet2wallet_error.php");

                } 
            return true;
        } catch (PDOException $e) {
            $this->pdo->rollBack();
            die($e->getMessage());
        }
    }

    /**
     * close the database connection
     */
    public function __destruct() {
        // close the database connection
        $this->pdo = null;
    }

}



 // test the transfer method
$obj = new TransactionDemo();


// transfer from account 1 to 2
$from = isset($_POST["from"]);
$to = isset($_POST["to"]);
$balance = isset($_POST["balance"]);

$obj->transfer ($_POST["from"], $_POST["to"], $_POST["balance"]);


我能够通过上面的代码解决问题,谢谢您的贡献

<div id="w2w" class="container jumbotron">
 <h3 class="card-title center jumbotron1" style="background-color: #f88f20; color: #fff; margin-top: -15px;"><i class="ace-icon fa fa-credit-card"></i> Wallet To Wallet</h3>

<form class="form-horizontal" action="ajax/makeTransaction.php" role="form" method = "POST">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-9">
<select id="inputState" type="number"  class="form-control" name="from" class="form-control">
<option selected><?php echo $_SESSION['user_id']; ?></option>
</select>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-9">
<input type="number"  class="form-control" name="to" placeholder="Receiver Agent Account ID" value="">
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-9">                                          
<input type='number'  class="form-control" name="balance" placeholder="Amount" value="">
</div>
</div>


<div class="form-group">
<div class="col-sm-offset-2 col-sm-9 center">
<button type="submit" name="transfer" class="outline-btn info">Transfer</button>
</div>
</div>
</form>              
</div>