我有一个需要修理的个人项目。下面的代码应该从工人帐户中提取一笔金额(借记帐户),但它会返回一个空格并显示错误。请根据具体情况我需要错误或错过步骤的帮助。我是php的新手 这是我的代码
// connect db
<?php
$username="root";
$server="localhost";
$dbname="workers_db";
$password="worded";
$conn=mysql_connect($server,$username,$password);
mysql_select_db($dbname) or die ("SERVER ERROR".mysql_error());
?>
// check
<?php
if(isset($_GET['withdraw']))
{
$Req="SELECT * FROM balance";
$query=mysql_query($Req);
$nums=mysql_fetch_assoc($query);
//echo $nums['CurrentB'];
$amount=$_GET['amount'];
$newB= ceil($nums['CurrentB']- $amount);
///////////////
if($newB>5000)
{
$amount=$_GET['amount'];
$withdrawal="UPDATE balance SET CurrentB=$newB,LastWithdraw=$amount,date=now() WHERE b_id=1; ";
$query2=mysql_query($withdrawal);
//$nums=mysql_fetch_assoc($query2);
if($query2)
{
$Req="SELECT * FROM balance";
$query=mysql_query($Req);
$nums=mysql_fetch_assoc($query);
echo "Your new current account balance is". ' '.$nums['CurrentB'];
echo "<br/>Your Last Withdrawal was ". ' '.$nums['LastWithdraw'];
echo "<br/>Your Last Withdrawal Date was on". ' '.$nums['date'];
}
}
else
{
echo 'INSUFFICIENT BALANCE TO CONTINUE THIS TRANSACTION';
}
///////////////
}
else
{
$Req="SELECT * FROM balance";
$query=mysql_query($Req);
$nums=mysql_fetch_assoc($query);
echo "Your current account balance is". ' '.$nums['CurrentB'];
}
?>
<?php
if(!isset($_GET['withdraw']))
{
?>
//the form
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="GET" enctype="multipart/form-data">
Amount to Withdraw:  <input type="text" name="amount">
<input type="Submit" name="withdraw" value="Proceed">
</form>
<?php
}
?>
//end
<br/><a href="index.php">EXIT</a>
答案 0 :(得分:0)
编辑,对您的代码进行了一些更改,添加了内联注释,希望有所帮助
<?php
$username="root";
$server="localhost";
$dbname="workers_db";
$password="worded";
$conn=mysql_connect($server,$username,$password);
mysql_select_db($dbname) or die ("SERVER ERROR".mysql_error());
if(isset($_GET['withdraw'])) {
$Req="SELECT * FROM balance"; // maybe you should you add a WHERE clause here
$query=mysql_query($Req);
$nums=mysql_fetch_assoc($query);
// what is the initial balance for this user? uncomment the line below and check the the numbers
// echo $nums['CurrentB'];
$amount=$_GET['amount'];
$currentBalance = $nums['CurrentB'];
$newBalance = ceil($currentBalance - $amount);
// what is the newBalance for this user? uncomment the line below and see
// print $newBalance;
// if the balance after withdrawal is more the 5000 proceed
if($newBalance > 5000) {
$amount=$_GET['amount'];
$withdrawal="UPDATE balance SET CurrentB=$newB,LastWithdraw=$amount,date=now() WHERE b_id=1; ";
$query2=mysql_query($withdrawal);
//$nums=mysql_fetch_assoc($query2);
if($query2) {
$Req="SELECT * FROM balance";
$query=mysql_query($Req);
$nums=mysql_fetch_assoc($query);
echo "Your new current account balance is". ' '.$nums['CurrentB'];
echo "<br/>Your Last Withdrawal was ". ' '.$nums['LastWithdraw'];
echo "<br/>Your Last Withdrawal Date was on". ' '.$nums['date'];
}
}
// else give out error message
else {
echo 'INSUFFICIENT BALANCE TO CONTINUE THIS TRANSACTION';
}
}
else {
$Req="SELECT * FROM balance"; // maybe you should add here a WHERE clause, ex. WHERE b_id=xx
$query=mysql_query($Req);
$nums=mysql_fetch_assoc($query);
echo "Your current account balance is". ' '.$nums['CurrentB'];
echo '<form action="'.$_SERVER['PHP_SELF'] .'" method="GET" enctype="multipart/form-data">
Amount to Withdraw:  <input type="text" name="amount">
<input type="Submit" name="withdraw" value="Proceed">
</form>';
echo '<br/><a href="index.php">EXIT</a>';
}
?>
如果您想检查可用余额以进行提款, 测试当前余额是否严格小于要提取的金额,如果是,则触发错误消息。
如果当前余额更大或相等,意味着剩余余额为零,则可以处理提款。
if ($nums['CurrentB']<$amount) echo "not enough money";
else {
// process the withdrawal
}
您正在进行的测试是检查剩余余额是否大于5000,如果为真,则处理余额,如果为假则给出错误。
您当然可以包含此强制性剩余余额,但在测试中,您需要检查初始余额是否远高于5000最低值。 例如:
if ($nums['CurrentB'] - $amount < 5000 ) echo "limit exceeded";
else {
// process the withdrawal
}
请确保您在初始余额和余额之间做出区别。