Bank

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1541 Views - Last Post: 19 June 2009 - 02:00 PM Rate Topic: -----

#1 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Bank

Posted 17 June 2009 - 03:37 PM

I've got it all to work but...It allows you to deposit more than you have and withdraw more than what you have in the bank...

bank.php
<?
$user ="SELECT bank FROM login WHERE id = '".$_SESSION["userid"]."'";
$result = mysql_query($user);
while ($row = mysql_fetch_assoc ($result)){
	   $bank = $row['bank'];

echo'
<table width="100%" align="center">
<tr><td> 
<table width"50" align="center" border="1" cellspacing="2" bordercolor="#000000">
<tr><td align="center" bgcolor="#666666" colspan="2"><b>Bank</b></td></tr>
<tr><td >Balance:</td><td align="right" valign="middle">$'.$bank.'</td></tr>
<tr><td><FORM METHOD="POST" ACTION="bankedit.php">
<input type="text" name="deposit" /></td><td><INPUT TYPE="submit" value = "Deposit"></td></tr>
<tr><td><FORM METHOD="POST" ACTION="bankedit.php">
<input type="text" name="withdraw" /></td><td><INPUT TYPE="submit" value = "Withdraw"></td></tr>
</table>


bankedit.php
$user ="SELECT username,money,bank FROM login WHERE id = '".$_SESSION["userid"]."'";
$result = mysql_query($user);
while ($row = mysql_fetch_assoc ($result)){
	   $username = $row['username'];
	   $money = $row['money'];
	   $bank = $row['bank'];
	
	
$deposit = ($_POST['deposit']);
if ($deposit > $money){
	header ('Location: bank.php');
   echo ' You do not have this much on hand';
   }
   if (ereg("^[0-9]+$", $deposit)) {
$newmoney = $money - $deposit;
$newbank = $bank + $deposit;
$updatemoney = "UPDATE login SET money = '$newmoney' WHERE id = '".$_SESSION["userid"]."'";
$moneyres = mysql_query($updatemoney);
$updatebank = "UPDATE login SET bank = '$newbank' WHERE id = '".$_SESSION["userid"]."'";
$bankres = mysql_query($updatebank);
   } else { 
   header ('Location: bank.php');
   echo ' Only numbers can be used in this field';
   }
   
$withdraw = ($_POST['withdraw']);
  if (ereg("^[0-9]+$", $withdraw)) {
$newmoney = $money + $withdraw;
$newbank = $bank - $withdraw;
if (newbank <0){
	   header ('Location: bank.php');
	   echo ' You do not have this amount in your bank.';
}
$updatemoney = "UPDATE login SET money = '$newmoney' WHERE id = '".$_SESSION["userid"]."'";
$moneyres = mysql_query($updatemoney);
$updatebank = "UPDATE login SET bank = '$newbank' WHERE id = '".$_SESSION["userid"]."'";
$bankres = mysql_query($updatebank);
   } else { 
   header ('Location: bank.php');
   echo ' Only numbers can be used in this field';
   }}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Bank

#2 LadyYepperz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 15-June 09

Re: Bank

Posted 17 June 2009 - 04:12 PM

i didnt see anything wrong in your script except it didnt look logical to me. hope this helps.

bankedit.php
$user ="SELECT username,money,bank FROM login WHERE id = '".$_SESSION["userid"]."'";
$result = mysql_query($user);
while ($row = mysql_fetch_assoc ($result)){
	   $username = $row['username'];
	   $money = $row['money'];
	   $bank = $row['bank'];
   
   
$deposit = ($_POST['deposit']);
if ($deposit > $money){
	header ('Location: bank.php');
   echo ' You do not have this much on hand';
   }
   else{
		 if (!ereg("^[0-9]+$", $deposit)) {
			 header ('Location: bank.php');
			 echo ' Only numbers can be used in this field';
		  }
		  else {
			 $newmoney = $money - $deposit;
			 $newbank = $bank + $deposit;
			 
			  $updatemoney = "UPDATE login SET money = '$newmoney' WHERE id = '".$_SESSION["userid"]."'";
			  $moneyres = mysql_query($updatemoney);
			  $updatebank = "UPDATE login SET bank = '$newbank' WHERE id = '".$_SESSION["userid"]."'";
			  $bankres = mysql_query($updatebank);
		 }

	 }


$withdraw = ($_POST['withdraw']);
  if (!ereg("^[0-9]+$", $withdraw)) {
   header ('Location: bank.php');
   echo ' Only numbers can be used in this field';
   }
  else {
	  $newmoney = $money + $withdraw;
	  $newbank = $bank - $withdraw;
		 if (newbank <0){
			header ('Location: bank.php');
			echo ' You do not have this amount in your bank.';
		 }
		 else {
			 $updatemoney = "UPDATE login SET money = '$newmoney' WHERE id = '".$_SESSION["userid"]."'";
			 $moneyres = mysql_query($updatemoney);
			 $updatebank = "UPDATE login SET bank = '$newbank' WHERE id = '".$_SESSION["userid"]."'";
			 $bankres = mysql_query($updatebank);
	   }
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 LadyYepperz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 15-June 09

Re: Bank

Posted 17 June 2009 - 04:19 PM

these two ifs in deposits id switch around
i
f ($deposit > $money){
	header ('Location: bank.php');
   echo ' You do not have this much on hand';
   }
   else{
		 if (!ereg("^[0-9]+$", $deposit)) {
			 header ('Location: bank.php');
			 echo ' Only numbers can be used in this field';
		  }

make sure numbers are entered before checking the amount.

This post has been edited by LadyYepperz: 17 June 2009 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Bank

Posted 17 June 2009 - 04:19 PM

thanks a lot it works now :)
Was This Post Helpful? 0
  • +
  • -

#5 LadyYepperz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 15-June 09

Re: Bank

Posted 17 June 2009 - 04:29 PM

you're very welcome! glad i could help!
Was This Post Helpful? 0
  • +
  • -

#6 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Bank

Posted 17 June 2009 - 05:02 PM

ok I'm stuck on transferring money between accounts. I have it so when it transfers the money it deducts it from the original player but it doesn't go to the player you send it to.

$transfer = ($_POST['amount']);
if ($transfer > $money){
	header ('Location: bank.php');
   echo ' You do not have this much on hand';
   }
   else{
		 if (!ereg("^[0-9]+$", $transfer)) {
			 header ('Location: bank.php');
			 echo ' Only numbers can be used in this field';
		  }
		  else {
			 $newmoney = $money - $transfer;
			 
			  $updatemoney = "UPDATE login SET money = '$newmoney' WHERE id = '".$_SESSION["userid"]."'";
			  $moneyres = mysql_query($updatemoney);
					   		  
		}

	 }
	 
$usertransfer = ($_POST['user']); 
$finduser = "SELECT username,money FROM login where username = '$usertransfer'";
$userres = mysql_query($finduser);
while ($rowi = mysql_fetch_assoc ($userres)){
	   $usernamei = $rowi['username'];
	   $moneyi = $rowi['money'];
	   
	   $usermoney = $moneyi + $transfer;
	   $updateuser = "UPDATE login SET money = '$usermoney' WHERE id = '$usertransfer'";
	   $updateres = mysql_query($updateuser);
   }


Was This Post Helpful? 0
  • +
  • -

#7 LadyYepperz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 15-June 09

Re: Bank

Posted 17 June 2009 - 05:44 PM

$transferFrom = $_SESSION['userid'];
$transfer = $_POST['amount'];

if (!ereg("^[0-9]+$", $transfer)) {
			 header ('Location: bank.php');
			 echo ' Only numbers can be used in this field';
   }
   else{
		 if ($transfer > $money){
			 header ('Location: bank.php');
			 echo ' You do not have this much on hand';
		  }
		  else {
			 $newmoney = $money - $transfer;
			 
			  $updatemoney = "UPDATE login SET money = '$newmoney' WHERE id = '$transferFrom' ";
			  $moneyres = mysql_query($updatemoney);
								 
		}

	 }
	 
$transferTo = $_POST['user'];
$finduser = "SELECT username,money FROM login where username = '$transferTo'";
$userres = mysql_query($finduser);
while ($rowi = mysql_fetch_assoc ($userres)){
	   $usernamei = $rowi['username'];
	   $moneyi = $rowi['money'];
	   
	   $usermoney = $moneyi + $transfer;
	   $updateuser = "UPDATE login SET money = '$usermoney' WHERE username = '$transferTo'";
	   $updateres = mysql_query($updateuser);
   }




there were two errors
1. you called the $_session['userid'] wrong
2. line 3 (from the bottom) your query was for id instead of name.
Was This Post Helpful? 0
  • +
  • -

#8 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Bank

Posted 18 June 2009 - 03:46 AM

cheers for noticing the errors but it still doesn't want to send the cash to the other player :P
Was This Post Helpful? 0
  • +
  • -

#9 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Bank

Posted 18 June 2009 - 10:19 AM

Is there a reason you're using a while loop here? I would assume that you don't allow duplicate user names therefore this is always going to be a single result.

Have you echoed the $usermoney to see if at that point the calculation is correct?

It's kinda the shotgun method of troubleshooting, but it helps find the breakdown.
Was This Post Helpful? 0
  • +
  • -

#10 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Bank

Posted 18 June 2009 - 12:51 PM

how odd it's actually not picking up the user who the cash is being transferred to...Am I missing something?

bank:
<tr><td>Username:</td>
<td><FORM METHOD="POST" ACTION="bankedit.php">
<input type="text" name="user" /></td></tr>
<tr><td>Amount:</td>
<td>
<input type="text" name="amount" /></td></tr>
<tr><td colspan="2" align="center"><INPUT TYPE="submit" value = "Transfer"></td></tr>



bankedit:
$transferTo = ($_POST['user']);
echo $transferTo;


[edit] got it to work

This post has been edited by Decypher: 18 June 2009 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Bank

Posted 18 June 2009 - 01:41 PM

I'm having problems with displayin the last 10sent and last10 recieved part:

<?
$user ="SELECT bank FROM login WHERE id = '".$_SESSION["userid"]."'";
$result = mysql_query($user);
while ($row = mysql_fetch_assoc ($result)){
	   $bank = $row['bank'];
	   $username = $row['username'];

echo'
<table width="100%" align="center">
<tr><td>
<table width"50" align="center" border="1" cellspacing="2" bordercolor="#000000">
<tr><td align="center" bgcolor="#666666" colspan="2"><b>Bank</b></td></tr>
<tr><td >Balance:</td><td align="right" valign="middle">$'.$bank.'</td></tr>
<tr><td><FORM METHOD="POST" ACTION="bankedit.php">
<input type="text" name="deposit" /></td><td><INPUT TYPE="submit" value = "Deposit"></form></td></tr>
<tr><td><FORM METHOD="POST" ACTION="bankedit.php">
<input type="text" name="withdraw" /></td><td><INPUT TYPE="submit" value = "Withdraw"></form></td></tr>
</table>';

$bankid ="SELECT sender,receiver,amount FROM bank WHERE username = '$username ORDER BY id DESC LIMIT 10";
$bankres = mysql_query($bankid);
while ($rowi = mysql_fetch_assoc ($bankres)){
	   $sender = $row['sender'];
	   $receiver = $row['receiver'];
	   $amount = $row['amount'];
echo '	   
</td><td>
<table width"50%" align="center" border="1" bordercolor="#000000" cellspacing="2">
<tr><td align="center" bgcolor="#666666" colspan="2"><b>Last 10 Received</b></td></tr>
<tr><td>
if ($username = $receiver){
	'.$sender.'
}
</td><td>$amount</td></tr>
<tr><td align="center" bgcolor="#666666" colspan="2"><b>Last 10 Transfers</b></td></tr>
<tr><td>
if ($username = $sender){
	'.$receiver.'
}
</td><td>$amount</td></tr>
</table>';}


error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a6490674/public_html/Mafia/bank.php on line 38

This post has been edited by Decypher: 18 June 2009 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#12 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Bank

Posted 18 June 2009 - 03:43 PM

You used 2 different variables

while ($rowi = mysql_fetch_assoc ($bankres)){
	   $sender = $row['sender'];
	   $receiver = $row['receiver'];
	   $amount = $row['amount'];



$rowi and $row
Was This Post Helpful? 0
  • +
  • -

#13 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Bank

Posted 18 June 2009 - 05:28 PM

cheers for that. Seem to miss somethign as simple as that when you look at it for ages...

However I'm still gettin that error? any ideas why?
Was This Post Helpful? 0
  • +
  • -

#14 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Bank

Posted 18 June 2009 - 08:19 PM

Maybe a good idea to put some debug into action while developing ...

$bankres = mysql_query($bankid) or die (mysql_error());



just at a quick peek

$bankid ="SELECT sender,receiver,amount FROM bank WHERE username = '$username ORDER BY id DESC LIMIT 10";



should be

$bankid ="SELECT sender,receiver,amount FROM bank WHERE username = '$username' ORDER BY id DESC LIMIT 10";



you were missing a single quote around the fields value
Was This Post Helpful? 0
  • +
  • -

#15 LadyYepperz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 15-June 09

Re: Bank

Posted 18 June 2009 - 08:27 PM

You're echoing your code.


<tr><td>'.
if ($username = $receiver){
	$sender
	}.'
</td><td>$amount</td></tr>
<tr><td align="center" bgcolor="#666666" colspan="2"><b>Last 10 Transfers</b></td></tr>
<tr><td>'.
if ($username = $sender){
	$receiver
}.'
</td><td>$amount</td></tr>
</table>';}




If you're not using a code editor I suggest you get one. These small errors are easily noticed before you post your scripts to the web.

One free editor: Komodo Edit :but their are many. Good Luck!

This post has been edited by LadyYepperz: 18 June 2009 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2