我无法尝试使用存款功能更新余额。基本上,我每次在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>
答案 0 :(得分:0)
您没有将当前值写入任何内容,例如数据库或平面文件。您需要存储和检索值。尝试查看$ _SESSION并以此方式存储银行帐户值。