0

我在尝试使用存款功能更新余额时遇到问题。基本上,每次我以 html 格式提交要存入的金额时,我都会尝试更新我的 $balance 变量的值。问题是,它似乎只是更新下一个值而不是添加到余额中。

我已经通过添加值作为参数并多次调用该方法直接尝试了这个(不使用表单),它增加了我的总余额的值。我唯一的问题是尝试使用表单 $_Post 来传递作为参数输入的任何内容。

 <?php

class BankAccount{

    private $balance = 0;
    private $name='Bob';
    private $date;

    public function getName(){
        return $this->name; 
    }

    public function getDate(){
        $this->date = date("m.d.y");
            return $this->date; 
    }

    public function getBalance(){
        if(($this->balance) > 0){
            return $this -> balance;

        } else if(($this->balance) <= 0 ){
            return 'Balance is empty '; 
        }else{
            return 'error'; 
        }   
    }
        public function Deposit($amount){

        $this->balance = $this->balance + $amount;  
    }


    public function Withdraw($amount){

        if(($this->balance) < $amount){
            echo 'Not enough funds to withdraw. ';

        } else{
            $this->balance = $this->balance - $amount;  
        }
    }   
}
    $money=0;
    if(isset($_POST['deposit'])){ $money = $_POST['deposit'];}

    //Instance of class
    $bank = new BankAccount;

    //Get Balance 
    //echo $bank ->getBalance();

    //Deposit
    $bank ->Deposit(50);
    $bank ->Deposit(50);
    $bank ->Deposit($money);

    //Withdraw
    //$bank ->Withdraw(30);


?>

<html>
<body>
<form name="Bank" method="POST" style="margin-top:25px;">

<label style="color:red;">Name: </label><?php echo $bank ->getName();   ?><br>
<label style="color:red;">Date: </label><?php echo $bank ->getDate();   ?><br>
<label style="color:red;">Balance: </label><?php echo $bank->getBalance(); ?><br>

    <label>Deposit</label>
        <input type="text" name="deposit" maxlength="25"><br>
        <input type="submit" value="submit" ><br> 


</form>

</body>
</html>
4

1 回答 1

0

您没有将当前值写入任何内容,例如数据库或平面文件。您需要存储和检索该值。尝试查看 $_SESSION 并以这种方式存储银行帐户值。

于 2014-01-02T23:07:44.353 回答