简体   繁体   中英

Transfer amount from a user account to another user account

I have a database that contains users data like useraccountid , userid , balance , totalwithdrawal , creationdate . I have finished with the structure and the code but keep giving me no response and the fund on the sender account doesn't change.

<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>

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;
    }

}

?>

now I created a transaction.php and like it on the form action="" but I am not getting any error and the fund does not move.

what am I doing wrong?

you added wallet-to-wallet.php in this href.

<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>

Remove this or move the code from transaction.php and paste in wallet-to-wallet.php page.

 * 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"]);


i was able to solve the problem with this codes above,Thanks for your contribution

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM