updating database

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1049 Views - Last Post: 25 August 2008 - 01:19 PM Rate Topic: -----

#1 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

updating database

Posted 14 August 2008 - 04:35 AM

so im making a bank for my slots machine and ive put it into mysql database. i know how to update things but hove to get it to + an insuccsesful bet and - a succsessful bet

<?php
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("globalwars") or die(mysql_error());

$result = mysql_query("UPDATE slots SET bank='' WHERE bank=''") 
or die(mysql_error());  

?>


<?php
//shows how much money the bank has
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("globalwars") or die(mysql_error());

$bank = mysql_query("SELECT * FROM slots") or die(mysql_error());  

$row = mysql_fetch_array($bank) or die(mysql_error());
echo $row['bank'];
?>



Is This A Good Question/Topic? 0
  • +

Replies To: updating database

#2 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 14 August 2008 - 04:37 AM

Just to double check, are you wanting to store an amount of unsuccessful bets and successful bets the player has?
Was This Post Helpful? 0
  • +
  • -

#3 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 14 August 2008 - 04:40 AM

sorry should of explained that bit

the bank starts off with 1000
the player starts off with unlimited(for now)

if the player wins i need it to subtract $bet*3 from the cell 'bank' and if they loose i need it to add $bet to the cell 'bank'.

when ni tried to do this with php it kept putting the bank back to 1000 after evry bet so i figured the update power on mysql would be handy
Was This Post Helpful? 0
  • +
  • -

#4 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 14 August 2008 - 04:55 AM

$result = mysql_query("UPDATE slots SET bank='' WHERE bank=''") or die(mysql_error());

Means, UPDATE every row in the table 'slots' by setting the value in column bank to '' WHEN the value in column bank is equal to ''
So I would assume this doesn't update anything.

I would do something like this:

$playerid = 2; // say you have a table with the players info on it - it has an index called playerid. This players id is 2

//did player win or lose?
if($playerwin = 1){
	$playersnew = $bet*3;// will add money to player
	$bankssnew = ($bet*3)*-1; // will take the money off bank value - the *-1 makes it negative
}else{
	$bankssnew = $bet*3; // will add money to bank
	$playersnew = ($bet*3)*-1;// will take the money off player value - the *-1 makes it negative
}

$sql = 'SELECT banksmoney, playersmoney FROM playerinfo WHERE playerId = '.$playerid.' LIMIT 1';
$result = mysql_query($sql) or die(mysql_error());  

$row = mysql_fetch_array($result);

$updatedbankmoney = ( $row['banksmoney'] + $banksnew); // if $banksnew is negative this will take money off, even though it's + $banksnew
$updatedplayermoney = ($row['playersmoney'] + $playerssnew);// if $playerssnew is negative this will take money off, even though it's + $playersnew

$result = mysql_query("UPDATE playerinfo SET banksmoney=".$updatedbankmoney.", playersmoney = ".$updatedplayermoney." WHERE playerid=".$playerid) 
or die(mysql_error());  



*EDIT*

Let me know if that was too much for ya and I'll break it down and explain each part

This post has been edited by pemcconnell: 14 August 2008 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 14 August 2008 - 09:01 AM

errrm so i would need a table for player and table for bank ???


i get it apart from the very first bit with the $player win how do i make a if function a variable :S
Was This Post Helpful? 0
  • +
  • -

#6 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 14 August 2008 - 09:12 AM

If it is a 'one player', I would set up a table like this:

playerId | playerName | playerPassword | playersmoney | banksmoney

You can get the $playerwin on a postback:

i.e.
		<?php
		
		if(isset($_POST['submit'])){ //checks to see if the form has been submitted
			
			$playerwin = (int)$_POST['playerwin']; // can be an integer variable from your form

			$playerid = (int)$_SESSION['userid']; // You can get this from the user login

			//did player win or lose?
			if($playerwin = 1){
				$playersnew = $bet*3;// will add money to player
				$bankssnew = ($bet*3)*-1; // will take the money off bank value - the *-1 makes it negative
			}else{
				$bankssnew = $bet*3; // will add money to bank
				$playersnew = ($bet*3)*-1;// will take the money off player value - the *-1 makes it negative
			}
			
			$sql = 'SELECT banksmoney, playersmoney FROM playerinfo WHERE playerId = '.$playerid.' LIMIT 1';
			$result = mysql_query($sql) or die(mysql_error());  
			
			$row = mysql_fetch_array($result);
			
			$updatedbankmoney = ( $row['banksmoney'] + $banksnew); // if $banksnew is negative this will take money off, even though it's + $banksnew
			$updatedplayermoney = ($row['playersmoney'] + $playerssnew);// if $playerssnew is negative this will take money off, even though it's + $playersnew
			
			$result = mysql_query("UPDATE playerinfo SET banksmoney=".$updatedbankmoney.", playersmoney = ".$updatedplayermoney." WHERE playerid=".$playerid) 
			or die(mysql_error());  


		}
		
		?>
		

OR, if you want it in a function:
		<?php
		
		if(isset($_POST['submit'])){ //checks to see if the form has been submitted
			updateMoney((int)$_SESSION['userid'], (int)$_POST['playerwin']);
		}
		
		function updateMoney($playerid, $playerwin){

			//did player win or lose?
			if($playerwin = 1){
				$playersnew = $bet*3;// will add money to player
				$bankssnew = ($bet*3)*-1; // will take the money off bank value - the *-1 makes it negative
			}else{
				$bankssnew = $bet*3; // will add money to bank
				$playersnew = ($bet*3)*-1;// will take the money off player value - the *-1 makes it negative
			}
			
			$sql = 'SELECT banksmoney, playersmoney FROM playerinfo WHERE playerId = '.$playerid.' LIMIT 1';
			$result = mysql_query($sql) or die(mysql_error());  
			
			$row = mysql_fetch_array($result);
			
			$updatedbankmoney = ( $row['banksmoney'] + $banksnew); // if $banksnew is negative this will take money off, even though it's + $banksnew
			$updatedplayermoney = ($row['playersmoney'] + $playerssnew);// if $playerssnew is negative this will take money off, even though it's + $playersnew
			
			$result = mysql_query("UPDATE playerinfo SET banksmoney=".$updatedbankmoney.", playersmoney = ".$updatedplayermoney." WHERE playerid=".$playerid) 
			or die(mysql_error());  

			return true;
		}
		
		?>

<!-- display data -->
		

This post has been edited by pemcconnell: 14 August 2008 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#7 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 14 August 2008 - 09:18 AM

so far it is only one page and i dont have a user login also what is the player win bit ??
Was This Post Helpful? 0
  • +
  • -

#8 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 14 August 2008 - 09:26 AM

Ooops :( I just presummed you had a login system, soz. Just scrap the whole $playerid business.

playerwin is a trigger to tell the function if the player has won or not. if $playerwin = 1 then the function will add $bet*3 to the player and remove $bet*3 from the bank.

I gotta go - outta work in 5 mins :) but if you're still stuck, post all the code you have and I'll review it tomorrow.

Good Luck :P
Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 14 August 2008 - 09:29 AM

but the way i tell if the player has won or not is by doing


<?php 
function check($bet)
{
global $one;
global $two;
global $three;
if($one == $two && $one == $three)
echo $bet*3;
else
echo 0;
}
?>




anywho this is the whole code and i aint been doing this for long as you know .. and its only on my localhost and i dont have a login system

<html>

<head>
</head>
<body bgcolor="#FFFFCC">





<table cellspacing="1" cellpadding="0" border="1" border-color="#FFFFCC" bgcolor="#FFFFCC" id="layout" 

height="98%" width="98%">

<!--header-->
<tr height="20%">  
<td colspan="2" > 
<h1> 
<p align="center">
welcome to ......
</p>
</h1>
</td>
</tr>
<!--header-->

<!--links-->
<tr height="80%">
<td width="20%">
<!--links page-->
<?php require("links.php"); ?>
<!--links page-->
</td>
<!--links-->

<!--content-->
<td width="80%">
<p id="content" align="center">








<?php
$one=rand(0,3);
$two=rand(0,3);
$three=rand(0,3);


function slotpicture()
{
global $one;
global $two;
global $three;

print("<td width=\"33%\"><center>$one</td>");
print("<td width=\"33%\"><center>$two</td>");
print("<td width=\"33%\"><center>$three</td>");
if($one == $two && $one == $three)
{
print("<tr><td colspan=3 bgcolor=#008080><center>Winner!<br/>
<form method=\"POST\" action=\"page1.php\">
<input type=\"submit\" value=\"Spin!\">
</form>");
exit;
}
}
?>



  <p id="money"> bank : <?php echo $bank; ?> </p>


<?php
echo $bet;
?>

<div id="control"><center>
  <form action="page1.php" method="get">
  <div id="betSelector"> 


  <p id="bet"> bet: <input type="text" name="bet">




<?php
$bet = $_GET["bet"];
?>

</p>

</center></div>


<?php 

$bank = $bank;

?>



<?php
if($one == $two && $one == $three)
{
$bank = $bank-($bet*3);
}
else
{
$bank = $bank+$bet;
}
?>






<div align="center"><center>
you bet : <?php echo $bet; ?>
<br>
and you won : <?php check($bet); ?>


<?php 
function check($bet)
{
global $one;
global $two;
global $three;
if($one == $two && $one == $three)
echo $bet*3;
else
echo 0;
}
?>




<table border="1" width="50%">
  <tr>
<?
slotpicture();
?>
</td>
  </tr>
  <tr>
	<td width="100%" colspan="3" bgcolor="#CCCCCC">
<center>
<form method="POST" action="page1.php">
<input type="submit" value="spin!">
</form>
<p>
<input type="hidden" name="bank"   value="10"   />
<input type="hidden" name="bet"	value="20"   />
<input type="hidden" name="one"	value="50"   />
<input type="hidden" name="two"	value="100"   />
<input type="hidden" name="three"   value="200"   />
</p> 
</center>
  </form> 
  

	  </center>





</p>
</td>
</tr>
<!--content-->

</table>




</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#10 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 15 August 2008 - 04:16 AM

anyone other than pemcconnell know how to do it ???
Was This Post Helpful? 0
  • +
  • -

#11 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 15 August 2008 - 04:42 AM

What's wrong with my advice :(
Was This Post Helpful? 0
  • +
  • -

#12 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 15 August 2008 - 05:17 AM

yesterday you said you were busy and to post the whole code so you could analyze it today cos i wanted to work out how to update the database . re read the topic and youll get it

thanks
Was This Post Helpful? 0
  • +
  • -

#13 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 15 August 2008 - 05:34 AM

lol - i know. Was jus' kiddin :)

It's friday which means 'serious pete' is off today :P
Was This Post Helpful? 0
  • +
  • -

#14 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: updating database

Posted 15 August 2008 - 10:29 AM

ok well ill try it now i have my login system working :) well later now im off to teh xbox :)

do i make a new table then for the slots or do i add it to my users table ??
Was This Post Helpful? 0
  • +
  • -

#15 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: updating database

Posted 18 August 2008 - 01:49 AM

I'd make a new table with any reel / slot stats. It's good practise to keep separate categories in separate tables when database modelling.

I would do it as follows:

TABLE NAME: tblplayerinfo

EXAMPLE COLUMNS:

playerId (PK, AUTO-INCREMENT), playerName, playerUsername, playerPassword, playerEmail (etc... Fill with player info)

TABLE NAME: tblplayerstats

EXAMPLE COLUMNS:

statsId (PK, AUTO-INCREMENT), playerId (index this column), statsPoints, statsWins, statsLosses (etc... any info you may need reguarding the players statistics)

Obviously this could go on but I'm sure you get the idea.

Now that you have a login system, when the user successfully logs on, get his user id (eg. playerId in my example - a unique row identifier for that player) and assign it to a session (e.g. $_SESSION['userid'])

Now, when you need to find out the stats for the active player you can call something like this:

'SELECT statsPoints, statsWins, statsLosses FROM tblplayerstats WHERE playerId = '.(int)$_SESSION['userid'];



Good Luck :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2