PHP-Bank System For Games - Part 1
Hypertext Preprocessor (PHP)Welcome to our first tutorial posted on Tutorial2Life.com! :)
In this tutorial we are going to create a bank system, that can be used for php game development.
Please note, that you need some experience with XHTML, PHP and MYSQL.
Ok, Ready to start?
Step 1
First create a .php file, and open it up with your editor.
I'am a MAC user, so i use Coda, this editor can be found at panic.com
First add the php tags to your document and add a comment if you want.
How does the database look?
CREATE TABLE `users` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(180) NOT NULL,
`password` varchar(180) NOT NULL,
`money` varchar(180) NOT NULL,
`bank` varchar(180) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`, `password`, `money`, `bank`) VALUES
(1, 'MyUsername', 'MyPassword', '2500', '2500');
<?php
// PHP GAME - BANK system
?>
Step 2
Now its time to connect to our database.
To connect to a database we use this code
<?php
// PHP GAME - BANK system
//DATABASE INFORMATION
mysql_connect("localhost", "root", "") or die(mysql_error()); //We connect to a database using the mysql_connect command.
mysql_select_db("gphp-bank") or die(mysql_error()); //We select the database we are going to use.
?>
Functions Explained:
Mysql_Connect
Mysql_Select_DB
These are basics!!
All php games use MYSQL, so you cant build one without it.
Step 3
Now its time to create a form, where you can enter the amount of money etc..
echo ''<b>Welcome to PHP-BANK.'</b>'<br /> // Intro Text
Please enter the amount you want to store or withdraw'<br />'<br />'; //Description
$finance = mysql_query("SELECT * FROM users WHERE id='$id'") //SELECT user information
or die(mysql_error());
while($f = mysql_fetch_array( $finance )) { //Fetch User data from users table.
echo '<b>On Hand:</b>'.$f['money'].'<br />'; //Display Users Money
echo '<b>On Bank:</b>'.$f['bank'].'<br /><br />'; //Displays Users Bank Credit
}
echo '<form action="" method="post"> <br />
Money Amount: $<input type="text" name="amount" value="" /><br /><br />
<input type="submit" name="s" value="Store Money!" />
<input type="submit" name="w" value="Withdraw Money!" />
</form> '; //Form Input, Amount, and 2 buttons Store and Withdraw.
What you see here above:
- Intro Text, description about the bank system
- Select Users data from the database
- Display user information, Money on hand, and on the bank
- Form, where you can enter money amount and choose Store or Withdraw.
Now we are going to write some code between the userinfo and the database information:
<?
// PHP GAME - BANK system
//DATABASE INFORMATION
mysql_connect("localhost", "root", "") or die(mysql_error()); //We connect to a database using the mysql_connect command.
mysql_select_db("gphp-bank") or die(mysql_error()); //We select the database we are going to use.
//New Code Comes Here
//User Info, and Form below that new code.
?>
Now the first thing we are going to add between those codes is:
$id=1; //User ID, this can be replaced by the $_SESSION['username'] for example, so its dynamic.
This defines the users, so the script knows where to get the info from.
User ID: 1 is my test account, it has 2500 on hand and 2500 located at the bank.
Now this code comes in, inline explained:
if (isset($_POST['amount'])) { //Check if something gets posted.
$amount = strip_tags(addslashes($_POST['amount'])); //Money Amount to Store or Withdraw
if (!is_numeric($amount)) //Check if value is numeric
{
echo "Please enter numeric value.<br /><br /> ";
}
elseif ($amount < 0) //Check if value is positive
{
echo "Please enter positive value.<br /><br /> ";
}
else{
$result = mysql_query("SELECT * FROM users WHERE id='$id'") //SELECT user information
or die(mysql_error());
while($rows = mysql_fetch_array( $result )) { //Fetch user information
if(isset($_POST['s'])){ //Check if "s"(store) is posted
if($amount > $rows['money']){
echo 'You dont have that amount of money.<br /><br />';} //Check if user has enough money
else{
$subtract = mysql_query("UPDATE users SET money=money-'$amount' WHERE id='$id'")
or die(mysql_error()); //Remove money value from hand.
$add = mysql_query("UPDATE users SET bank=bank+'$amount' WHERE id='$id'")
or die(mysql_error()); //Add money value to bank
echo '$'.$amount.' has been transferd to your bank!<br /><br />'; //Display amount + message
} //Close Else
} //Close if POST 'S'
if(isset($_POST['w'])){ //Check if "w"(withdraw) is posted
if($amount > $rows['bank']){
echo 'You dont have that amount of money.<br /><br />';
} //Check if user has enough money in the bank.
else{
$subtract = mysql_query("UPDATE users SET bank=bank-'$amount' WHERE id='$id'")
or die(mysql_error()); //Remove the money value from the bank
$add = mysql_query("UPDATE users SET money=money+'$amount' WHERE id='$id'")
or die(mysql_error()); //Add the amount to the current money value
echo '$'.$amount.' has been transferd to your hand! <br /><br />'; //Display message + the amount that has been transferd
}//Close Else
}//Close If POST 'w'
}//Close No Errors
}//Close If POST 'Amount'
}
Thats it! I hope this tutorial will help you create stunning games with a bank system :P
Part 2 will come soon!
this is awesome. thanks.
Ian 12-05-08 @ 11:40
Your code is a good attempt, but I can just get it to work. Can u send me a complete workable version. Thanks
Adeyemi 12-11-09 @ 09:26