0

I'm trying to create an imaginary ATM for a schoolproject but I'm stuck on something.
Everything works fine but I can take more money from my account than I have.

For example:
I have 1300 euro's in the database and I input 1400 euro's my system happily updates my balance from 1300 to -100.

And that's exactly what I don't want to happen. My code right no looks like this:

if (( /* i have no clue what comes here */ -$bedrag)>0)   // This should become something like: IF "value from database" > "amount"   (bedrag=amount btw)
                {
                echo "Het pinnen is gelukt!"; 
                $pinnen= "UPDATE rekeningen SET Saldo= Saldo - '$bedrag' WHERE Rekeningnr ='$nummer'";
                mysql_query($pinnen);
                }
4

3 回答 3

0

假设你有一个变量,比如说,$current_balance在操作之前存储帐户的余额,你可以这样:

if ($bedrag > $current_balance) {
 echo "You don't have enough money, sir!";
}
else {
 echo "Het pinnen is gelukt!"; 
 $pinnen= "UPDATE rekeningen SET Saldo= Saldo - '$bedrag' WHERE Rekeningnr ='$nummer'";
 mysql_query($pinnen);
}

此外,最好对Saldo表中的字段设置检查约束以避免负值。只是说。

于 2013-03-13T21:01:31.330 回答
0

我认为您想添加,而不是减去您的金额UPDATE

UPDATE rekeningen SET Saldo= Saldo + '$bedrag' WHERE Rekeningnr ='$nummer'
                                   ^

然后,您可以使用负值进行提款。

于 2013-03-13T20:48:16.727 回答
0

您应该处理这个应用程序端。查询页面加载所需的信息,并将值保存在 sessionStorage 中:

window.onload = function() {
    sessionStorage.saldoval = "<?php $return['Saldo'] ?>";
}

然后在提交之前使用 Javascript 验证它。

var saldo = document.getElementById('SaldoInput');

if(saldo.value > sessionStorage.saldoval){
    // do something to tell the user they're naughty
    return false;
}

您不应该依赖数据库来执行这种验证。

于 2013-03-13T20:48:40.413 回答