2015-07-22 117 views
-2

我有一個需要修復的個人項目。下面的代碼應該從工作人員帳戶中提取一筆金額(借記帳戶),但它會返回空白並顯示錯誤。請根據具體情況,提供錯誤或遺漏步驟的幫助。我很新的PHP
這裏是我的代碼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: &emsp;<input type="text" name="amount"> 
<input type="Submit" name="withdraw" value="Proceed"> 
</form> 
<?php 
} 
?> 
//end 
<br/><a href="index.php">EXIT</a> 
+0

顯示的錯誤是什麼? –

+0

這是顯示「不足的餘額繼續此交易」。預計會顯示賬戶= <5000. – Alama

+0

@Alex的內容包括b_id,CurrentB,Withdraw,日期 – Alama

回答

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: &emsp;<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 
} 

只要確保你讓初始餘額和剩餘之間的差額。

+0

好的,@Alex,但我不知道在哪裏添加行? – Alama

+0

我可以編輯您的完整代碼,但您想如何繼續?你需要至少5000儲備? –

+0

是最少5000 – Alama