Author Topic: BBG using PHP + MySql Help? ;D  (Read 5202 times)

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
BBG using PHP + MySql Help? ;D
« on: July 31, 2007, 04:43:45 PM »
Ok
I have made a few flash games and other games in the past and have only made 1 online flash game which was ok

I can always easily learn flash but thats not what I'm here to do

I have wanted to create a BBG game for quite some time and have only just got back to it.
I have the basic knowledge of things and have made some PHP scripts for getting statistics of the website (page hits etc) creating a poll and creating a shoutbox with help from a few sites

I have some experience with MySql and not enough experience with PHP

I am planning to make a simple space BBG
The basics of the game are

Registering a new account without the need for confirming email
Logging in without having to have a confirmation code ^

Resources: Funds, Iron, Fuel
Funds + Iron: Used for building ships
Fuel: Used for travelling (Having trading ships travel to a planet to mine for iron and having warships travel to attack players)

Iron is obtained by mining planets with Trade Ships
Fuel is obtained by Buying it from ship hardware
Funds are obtained by attacking other players

When attacking other players, if you win (more ship power by more ships) you take a percentage of the attacked players funds and a percentage of their ships are destroyed (Percentage is worked out by how much more powerful your ships were)
Example:
if you have 1000 power and they have 500 you win
1000 - 500 = 500
500/20 = 50%
So 50% of their funds will be deducted
If they had 1,000,000 funds, they will now have 500,000

The outcome of mining is based on the amount of Trade Ships you have.
1Trade ship = 1 to 3 iron (random value between both numbers)
2Trade ship = 3 - 6 iron (random value between both numbers)
As the value of ships increase by 1 the min and max value of iron gain increases by 3

The only 2 available ships for now (because I'm making this basic for now) would be Trade Ships and War Ships

Cost of ships:
1Trade Ship - 500 Funds 250 Iron
1War Ship - 1,000 Funds 500 Iron

Resource Cost:
1Fuel - 5 Funds

Starting Resources:
Funds: 50,000
Fuel: 50,000
Iron: 25,000

Also I would like a score board
Score is found by Shiper power/ticks played (Amount of ticks passed since account creation)

Everything is based on ticks
1 Tick = 1minute

Attacking takes 3ticks for the fleet to head to target (deduct 10% of fuel) attacking target (with outcome) heading back to base (deduct 10% of fuel)

Trading also takes 3ticks for the fleet to head to planet (deduct 10% of fuel) mining at planet (with with random outcome) heading back to base (deduct 10% of fuel and add the mined iron on to current amount)


Could someone make a script for the basics of this so I can follow it and work on it?
« Last Edit: July 31, 2007, 04:50:19 PM by Lp9Sc »

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #1 on: July 31, 2007, 07:11:11 PM »
Hello there

First things first: its 1:18am here so please excuse any mistakes that might occur in what is written after this.

You already know some PHP, right? Then you won't need a complete script to make your game work. I will however try to provide a detailed guide on how to do what. I will divide the post in sections. Each section is marked with a title and ends with a horizontal bar and each section will include what forms you'll need, what PHP shall do with the provided informaion and what your MySQL database should do. At the beginning of each new section I will write the name of the file.

Before you start
Before we start we will need some basic MySQL tables to store all the important data.
We'll need to store the following information:
  • User Name
  • User Password
  • Resources
  • Ships
  • Ticks Played
  • Ticks accumulated

Since we are talking about a really simple game, we will also follow a really simple table layout. What you have here can be achieved by creating 2 tables (it would work with only 1, but 2 is simply a bit more organized)

MySQL
Table 1: users
The users-table will contain the data that is not directly affected by the game. This includes name, password, the ID of which user (we will need this to identify each user uniquely and it's better than using the name).
Code: [Select]
users
    user_id (primary, auto_increment)
    user_name
    password

Table 2: game
The game-table will contain all the data that is relevant to the game. To identify which user needs which game-entry we will specify a field called user which refers to the user_id. I will add another field called last_tick to the already specified fields which we will need later on.
Code: [Select]
game
    user
    funds
    iron
    fuel
    warship
    tradeship
    last_tick
    ticks
    ticks_used



Registering a new account

register.php

Forms
You said you didn't want to send an activation-key nor any other form of confirmation. This will simplify a lot of things.
The forms we will need are simple: 1 for the name and 1 for the password (if you trust in the stupidity of humanity, add an additional one for password-confirmation)

<form action='register.php' method=POST>
  <
input type=text size=15 name='name'Name
  
<input type=password mask='x' name='pw' size=15Password
  
<input type=submit>
</
form>


PHP
First we need to get the information from the fields. In register.php we will do the following:

if(isset($_POST['name']) AND isset($_POST['pw']))
{
  
$name striptags($_POST['name']);
  
$pw $_POST['pw'];
//END if(isset($_POST['name']) AND isset($_POST['pw'])) 


Now that we have this, we will add a bit of a security to the password. In the same if-statement do the follwing:

  $salt 
"random_sentence_here";
  
$pw md5($wp.$salt);

This will add another layer of security to the users-password.

Now that we have all the data we need, we insert them into the db.

MySQL
Again, we keep things simple. Just add the data in the if-statement into the db and leave the ID empty.

$sql 
"INSERT INTO users(name,password) VALUES('$name','$pw')";
mysql_execute($sql);


Now we need to add a entry for this user in the game-table. We will need some additional info like the current time, etc.

$now 
date("U");
$sql "INSERT INTO game(ID,funds,fuel,iron,last_tick,ticks) 
VALUES(LAST_INSERTED_ID(),'50000','50000','25000','
$now','100')";
mysql_query($sql);

I used the LAST_INSERTED_ID() statement to get the ID we automatically assigned the user in the previous mysql-statement. All the others are your specifications (fuel, etc.) I just added an additional starting-bonus of 100 ticks. I also added the current time as the time the user recieved ticks for the last time. From now on, we will use this time to constantly calculate the new amount of ticks.

register.php
So, this is what your register.php file should contain:

<html>
<?
php
if(isset($_POST['name']) AND isset($_POST['pw']))
{
  
$name striptags($_POST['name']);
  
$pw $_POST['pw'];
  
$salt "random_sentence_here";
  
$pw md5($wp.$salt);

  
$sql "INSERT INTO users(name,password) VALUES('$name','$pw')";
  if(
mysql_execute($sql))
  {
    print 
"User registration complete <br/><br/>";
  }
  
$now date("U");
  
$sql "INSERT INTO game(user,funds,fuel,iron,last_tick,ticks) 
  VALUES(LAST_INSERTED_ID(),'50000','50000','25000','
$now','100')";
  if(
mysql_query($sql))
  {
    print 
"Game registration complete <br/>
    You can now log into the game with your specified name and password"
;
  }
//END if(isset($_POST['name']) AND isset($_POST['pw'])) 
?>
<body>
<form action='register.php' method=POST>
  <input type=text size=15 name='name'> Name
  <input type=password mask='x' name='pw' size=15> Password
  <input type=submit>
</form>
</body>
</html>




Logging in

login.php

Forms
The forms we will need to get the users information are again, pretty clear. We'll need name and password. You know how the form looks from above.

PHP
The PHP now is again, pretty simple. I will use a session to check wheter the user is logged in or not. As soon as we have the information from the form, we check it against the users-table:

$name 
$_POST['name'];
$salt "random_sentence_here";
$pw md5($_POST['pw'].$salt);

$sql "SELECT user_id FROM users WHERE name='$name' AND password='$pw'";
if(
$rs mysql_fetch_array(mysql_query($sql)))
{
  
$_SESSION['user'] = $name;
  
$_SESSION['pw'] = $pw;
}
// END if($rs = mysql_fetch_array(mysql_query($sql)))


MySQL
No data-manipulation is needed here.

Once we specify the sessions, we will need to redirect them to the main page.

Now, on the main-page (and every other page only the user has access to) we need to check the sessions. If they aren't set or there is no such user, then we won't display any content and redirect them to login.php.

Forms
No Forms needed

PHP
How do we check if the session is valid? Pretty much the same as above:
1st, we need to see if a session is set:

if(!session_is_registered('user') OR !session_is_registered('pw'))
{
  
//there are no sessions set, redirect them to login.php
//END if(!session_is_registered('user') OR !session_is_registered('pw'))
elseif(!mysql_query("SELECT user_id FROM users WHERE name='$_SESSION[user]' AND password='$_SESSION[pw]'"))
{
  
//There is no such user with $_SESSION[user] and $_SESSION[pw] combination
  //redirect to login.php
//END elseif(!mysql_query("SELECT user_id FROM users WHERE name='$_SESSION[user]' AND password='$_SESSION[pw]'"))
else
{
  
//Display content
  // Location: content <- remember this location as I will need it later on
}


MySQL
We will need some MySQL on each content-page. This MySQL will be needed to update the ticks the user has. This section is a bit of a mix between PHP and MySQL, but it fit better into the MySQL departement. Anyways.
We have now validated that there is such a user as sepcified by the sessions, now we will need his information:

$id 
mysql_fetch_array(mysql_query("SELECT user_id,name WHERE name='$_SESSION[name] AND password='$_SESSION[pw]'"));
$user mysql_fetch_array(mysql_query("SELECT * FROM game WHERE user='$id[user_id]'"));

Now we have all the game-related data from the user in the $user-array.

I wanted to continue on this, but I've been writing for the better part of an hour and it's now past 2am. I will continue on this as soon as I find the time.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #2 on: August 01, 2007, 03:28:28 AM »
Since you already know PHP, you should study some game scripts to see how they work, then if you need help with certain aspects of the coding, we can help. You've written up quite a lot up there, and it seems like it could take a lot of time to code it all :)
Make a list of the main features you want, such as login/registration, ship building, mining, inventory, etc. If you don't know how to proceed on one of those features, we can help you out.

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #3 on: August 01, 2007, 03:35:56 AM »
Hm... I think what Zeggy suggested is the best. I was really on a helpful trip yesterday so that's why I started with a big response. When I think it over, it's probably best if you ask a more specific question rather than asking for a whole script.



And since my mood hasn't really changed I provide the links to a four-part tutorial which might help you. It mainly covers the very basis of php game development.

Tutorial Part I
Tutorial Part II
Tutorial Part III
Tutorial Part IV
note: I didn't write these. Someone else did.

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #4 on: August 01, 2007, 07:18:32 AM »
Thanks sinzygy, thats really helpful :]

My friend let me have the scripts to his game so I could learn from it but he didn't make it detailed for what things mean ;p
And his game was based on something totally different from what I wanted mine to be like
It didn't contain ticks

I have another friend that owns one and a populated online flash game (about 500 people play it) but he wont give me the script even for learning purposes xd

I'll get started on it right now and I'll come back here with results ^^

The good news is I heard that PHP is one of the easiest programming languages available ;o
If anything is easier than flash then it should be easy to learn! ;D
Well.. graphical flash wasn't soo easy but scripting everything pretty much just stayed in my head

I have quite a few PHP tutorials to read now :]
« Last Edit: August 01, 2007, 07:23:10 AM by Lp9Sc »

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #5 on: August 01, 2007, 07:44:24 AM »
Quote from: Lp9Sc
The good news is I heard that PHP is one of the easiest programming languages available ;o

Yep, PHP is really easy to learn. When I decided to make a PBBG with PHP + MySQL I spent about one week surfing the net and reading and then I already made my first game. It was heavily influenced by Killmonster, but I wrote it from scratch. And I didn't have any programming background back then.

My friend let me have the scripts to his game so I could learn from it but he didn't make it detailed for what things mean ;p
Well, you could post the script and ask us about what things mean ;)
*Hehe, don't fall for my cheap tricks to get a free script ^^

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #6 on: August 01, 2007, 08:54:41 AM »
Yeh sure but its pretty huge... Ok..

AddBan.php (Bans a user I'm guessing ;p)

Code: [Select]
<?
/*
This file and all others are protected by
a GPL.  Please read the licence before
editing these files.

-Voided Alliance, Copyright Auburnflame Games
*/

include('includes/functions.php');

connectDB();

$banLength = date('ymdH');
$toG = date('i') + $_POST['length'];
if ($toG < 10) {
$banLength .= "0" . (date('i') + $_POST['length']);
} else {
$banLength .= (date('i') + $_POST['length']);
}

$query = "SELECT * FROM va_users";
$result = mysql_query($query);
$num1 = mysql_numrows($result);

for ($i = 0; $i < $num1; $i++) {
if (mysql_result($result,$i,"user") == $_COOKIE['va_users']) {
$access = mysql_result($result,$i,"access");
}
}


if ($access > 1) {
$query2 = "SELECT * FROM va_users";
$result2 = mysql_query($query2);
$num2 = mysql_numrows($result2);

for ($i = 0; $i < $num2; $i++) {
if (strtolower(mysql_result($result2,$i,"user")) == strtolower($_POST['user'])) {
$uID = mysql_result($result2,$i,"id");
$found = true;
}
}

if ($access == 2 and $_POST['length'] <= 10) {
$good = true;
}
if ($access == 3 and $_POST['length'] <= 60) {
$good = true;
}
if ($access > 3) {
$good = true;
}

if ($found == true and $good == true) {
$query = "INSERT INTO va_bans VALUES ('','" . $uID . "','" . $banLength . "')";
mysql_query($query) or die("Unable to create ticket!");
}
header("Location: index.php?page=cp");
}
else
{
header("Location: index.php?page=cp");
}
?>

?>


AddBook.php

Code: [Select]
<?
/*
This file and all others are protected by
a GPL.  Please read the licence before
editing these files.

-Voided Alliance, Copyright Auburnflame Games
*/

include('includes/functions.php');

$connected = connectDB();
if ($connected == true) {
$name = $_POST['bName'];
$desc = $_POST['bDesc'];
$author = $_POST['bAuthor'];
$text = $_POST['bText'];
$pass = md5($_POST['bPass']);

$aPass = md5("123abc");

if ($aPass == $pass) {

$query = "INSERT INTO va_books VALUES ('','" . $name . "','" . $author . "','" . $desc . "','" . $text . "')";
mysql_query($query) or die("Unable to create Mailbox.");
header("Location: http://www.windmillent.com/game/index.php?page=game");
die();
}

//END Write!
} else {
header("Location: index.php?page=game");
die();
}

?>

AddBounty.php

Code: [Select]
<?
/*
This file and all others are protected by
a GPL.  Please read the licence before
editing these files.

-Voided Alliance, Copyright Auburnflame Games
*/

include('includes/functions.php');

connectDB();

//$que = "SELECT * FROM va_users WHERE user='" . $_POST['user'] . "' AND pass='" . md5($_POST['pass']) . "'";
$query = "SELECT * FROM va_users";
$result = mysql_query($query);
$num1 = mysql_numrows($result);

$query2 = "SELECT * FROM va_ticket";
$result2 = mysql_query($query2);
$num2 = mysql_numrows($result2);

$name = $_COOKIE['va_users'];
$amount = $_POST['bounty'];

for ($i = 0; $i < $num1; $i++) {
if (strtolower(mysql_result($result,$i,"user")) == strtolower($_POST['recieve'])) {
$userID = mysql_result($result,$i,"id");
$found = true;
}
if (strtolower(mysql_result($result,$i,"user")) == strtolower($name)) {
$user2ID = mysql_result($result,$i,"id");
$credits = mysql_result($result,$i,"currency");
}
}

if ($credits >= $amount and $amount > 0 and $found == true) {
$NC = $credits - $amount;
$query = "UPDATE va_users SET currency='" . $NC . "' WHERE id='" . $user2ID . "'";
mysql_query($query) or die("Unable to edit currency.");

$query = "INSERT INTO va_bounty VALUES ('','" . $user2ID . "','" . $userID . "','" . $amount . "')";
mysql_query($query) or die("Unable to create bounty!");

header("Location: index.php?page=game&gamepage=bca");
die();
} else {
header("Location: index.php?page=game&gamepage=bca2");
die();
}
?>

AddGuild.php

Code: [Select]
<?
/*
This file and all others are protected by
a GPL.  Please read the licence before
editing these files.

-Voided Alliance, Copyright Auburnflame Games
*/

include('includes/functions.php');

$connected = connectDB();
if ($connected == true) {
$query = "SELECT * FROM va_users";
$result = mysql_query($query);
$num1 = mysql_numrows($result);

$query2 = "SELECT * FROM va_guilds";
$result2 = mysql_query($query2);
$num2 = mysql_numrows($result2);

$guildN = $_POST['name'];
$guildP = $_POST['pass'];
$guildD = $_POST['describe'];
$cost = 1000;

for ($i = 0; $i < $num1; $i++) {
if (mysql_result($result,$i,"user") == $_COOKIE['va_users']) {
$credits = mysql_result($result,$i,"currency");
$id = mysql_result($result,$i,"id");
$cGid = mysql_result($result,$i,"guild");
}
}

for ($i = 0; $i < $num2; $i++) {
if (mysql_result($result2,$i,"name") == $guildN) {
$aEx = true;
}
}


for ($i = 2; $i < 9999999; $i++) {
for ($i2 = 0; $i2 < $num2; $i2++) {
if (mysql_result($result2,$i2,"id") == $i) {
$istakenMB++;
}
}
if ($istakenMB < 1) {
$createdID = $i;
break;
}
else
{
$istakenMB = 0;
}
}

$guildID = $createdID;

if ($cGid == 0 or $cGid == "") {
if ($credits >= $cost) {
if ($aEx != true) {
$UC = $credits - $cost;
$query = "UPDATE va_users SET currency='" . $UC . "' WHERE id='" . $id . "'";
mysql_query($query) or die("Unable to edit Credits!");

$query = "UPDATE va_users SET guild='" . $guildID . "' WHERE id='" . $id . "'";
mysql_query($query) or die("Unable to edit Guild!");

$query = "INSERT INTO va_guilds VALUES ('" . $createdID . "','" . $guildN . "','" . $id . "','" . $guildD . "','','" . $guildP . "')";
mysql_query($query) or die("Unable to create guild!");

header("Location: index.php?page=game&gamepage=guS");
} else {
//Guild Already Exists
header("Location: index.php?page=game&gamepage=guF");
}
} else {
//Not Enough Credits
header("Location: index.php?page=game&gamepage=guF");
}
} else {
//Already in a guild!
header("Location: index.php?page=game&gamepage=guF");
}

//Ending! DO NOT ADD CODE AFTER THIS LINE!
} else {
header("Location: index.php?page=game&gamepage=guF");
}
?>

AddItem.php

Code: [Select]
<?
/*
This file and all others are protected by
a GPL.  Please read the licence before
editing these files.

-Voided Alliance, Copyright Auburnflame Games
*/

include('includes/functions.php');

$connected = connectDB();
if ($connected == true) {

$stat = $_POST['stat'];

$query = "SELECT * FROM va_users";
$result = mysql_query($query);
$num1 = mysql_numrows($result);

for ($i = 0; $i < $num1; $i++) {
if (mysql_result($result,$i,"user") == $_COOKIE['va_users']) {
$access = mysql_result($result,$i,"access");
}
}


if ($access > 3) {
$query = "SELECT * FROM va_items";
$result = mysql_query($query);
$num1 = mysql_numrows($result);

for ($i = 15; $i < 9999999; $i++) {
for ($i2 = 0; $i2 < $num1; $i2++) {
if (mysql_result($result,$i2,"id") == $i) {
$istakenMB++;
}
}
if ($istakenMB < 1) {
$ItemID = $i;
break;
}
else
{
$istakenMB = 0;
}
}

if ($_POST['isbuffed'] == true) {
$query = "INSERT INTO va_items VALUES ('" . $ItemID . "','" . $_POST['rareroll'] . "','" . $_POST['itemName'] . "','" . $_POST['itemDescribe'] . "','" . $_POST['itemimage'] . "','" . $_POST['itemtype'] . "','" . $_POST['itemAP'] . "','1','" . $_POST['buffImage'] . "','" . $_POST['buffName'] . "','" . $_POST['buffBL'] . "','" . $_POST['buffSTR'] . "','" . $_POST['buffDEF'] . "','" . $_POST['buffHP'] . "','" . $_POST['buffMP'] . "')";
mysql_query($query) or die("Unable to create item!");
} else {
$query = "INSERT INTO va_items VALUES ('" . $ItemID . "','" . $_POST['rareroll'] . "','" . $_POST['itemName'] . "','" . $_POST['itemDescribe'] . "','" . $_POST['itemimage'] . "','" . $_POST['itemtype'] . "','" . $_POST['itemAP'] . "','0','" . $_POST['buffImage'] . "','" . $_POST['buffName'] . "','" . $_POST['buffBL'] . "','" . $_POST['buffSTR'] . "','" . $_POST['buffDEF'] . "','" . $_POST['buffHP'] . "','" . $_POST['buffMP'] . "')";
mysql_query($query) or die("Unable to create item!");
}
header("Location: index.php?page=cp");
}

} else {
header("Location: index.php?page=cp");
}

?>

AddNews.php

Code: [Select]
<?
/*
This file and all others are protected by
a GPL.  Please read the licence before
editing these files.

-Voided Alliance, Copyright Auburnflame Games
*/

include('includes/functions.php');

connectDB();

$subject = $_POST['subject'];
$text = $_POST['text'];
$user = $_COOKIE['va_users'];
$date = date("n.j.y");

$query = "SELECT * FROM va_users";
$result = mysql_query($query);
$num1 = mysql_numrows($result);

for ($i = 0; $i < $num1; $i++) {
if (mysql_result($result,$i,"user") == $_COOKIE['va_users']) {
$access = mysql_result($result,$i,"access");
}
}

if ($access > 3) {
$good = true;
}

if ($good == true) {
$query = "INSERT INTO va_news VALUES ('','" . $subject . "','" . $text . "','" . $user . "','" . $date . "')";
mysql_query($query) or die("Unable to create news!");
header("Location: index.php?page=cp");
die();
}
else
{
header("Location: index.php?page=cp");
die();
}

?>


Theres way too many of these xd!!!!
Theres things like
config.php ofcourse
Changepass.php
login
logout
sellitem
sendmessage
timer
spellinfo
donatecredits
editstats
editshopinv
psearch
joinguild
etc etc etc xd

His game is pretty good because admins can edit the items in the shop and prices etc by going on the admin panel, you do not need to go into the script and do it, I'm not sure if this is new or not but ya... xd


I've been following those tutorials and I'm doing ok with a basic BBG
It talks about chronjob which I am enable to do yet until my friend comes online because I need the client for MySql,
I downloaded MySql but it seems to be messed up! (I uninstalled it because I took time off from making websites etc for a few months but got hosted again a few days ago to make a BBG)
I'm not sure I have the right client for MySql tables, I forgot what I used last time ;s but with the one I have currently it doesn't seem to be working, it has instance confirguration, when I configure that it does not take me to the login for tables and I do not have any files for that =\
I'm gonna ask my webmaster when he comes online (My webmaster is also the person who gave me those scripts for his game lol) He hosts me for free ;D
« Last Edit: August 01, 2007, 09:01:24 AM by Lp9Sc »

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #7 on: August 01, 2007, 09:36:21 AM »
lol, you don't need to post all the files, or even the whole files. Just look through them as you improve your PHP, and if you don't understand certain sections, post them and we'll explain it for you.
Unless you'd like us to explain all of it, which is fine too :D

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #8 on: August 01, 2007, 10:18:20 AM »
Lp, I was only joking :)

you need to read the smallprints:
Quote from: Sinzygy himself from my last post in this topic
Well, you could post the script and ask us about what things mean ;)
*Hehe, don't fall for my cheap tricks to get a free script ^^
But as it seems it's udner the GPL so it's not too bad if we see the code.

About your MySQL problem: I'm guessing  you talk about installing MySQL on your local machine. To help you we need some more information, like operating system, etc. Maybe there was an installation error or something.

His game is pretty good because admins can edit the items in the shop and prices etc by going on the admin panel, you do not need to go into the script and do it, I'm not sure if this is new or not but ya... xd
Well most PBBG-Devs recommend making an admin menu for such purposes. But I'm always too lazy and just edit the needed data via phpmyadmin.
Btw: If you make a game with items, you want them to be stored in a database and not in the script itself. This will allow for easier manipulation and addition of data.
« Last Edit: August 01, 2007, 10:34:19 AM by Sinzygy »

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #9 on: August 01, 2007, 10:56:38 AM »
Ermm
I've had MySql before so it wouldn't be a compatibility problem ;o
Could you give me the download link for the right client? I don't think I had the right one

I am riddled by these errors

Warning: Cannot modify header information - headers already sent by (output started at /home/auburnf/public_html/lp9sc/GGMPOG/ggmpog.php:7) in /home/auburnf/public_html/lp9sc/GGMPOG/ggmpog.php on line 157

I have about 6 different ones of those and I'm not sure what it means
(Thats not my BBG that I'm working on, its just a random error that I'm often getting when doing anything xd)

Also I have authentication errors for accessing MySql, I think that would be because I have not yet created the tables in MySql because I havn't yet had the chance to logn to create the tables ;p

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #10 on: August 01, 2007, 11:41:25 AM »
Quote
Warning: Cannot modify header information - headers already sent by (output started at /home/auburnf/public_html/lp9sc/GGMPOG/ggmpog.php:7) in /home/auburnf/public_html/lp9sc/GGMPOG/ggmpog.php on line 157

those look like typical errors if you use variables before session_start(); Check where you start your sessions and move them to the top. Those can also be errors if you use header() wrong. or sometimes it can be caused by the wrong usage of header("Location: "yoursite).

sorry if im unclear, it's national celebration day here and I already got a wee bit of beer intus ;)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #11 on: August 01, 2007, 12:19:42 PM »
Yep, sinzygy's right.
If you use the header() function, you cannot have outputted something to the browser yet. You can have server-side code before it, but nothing should be outside the PHP tags and nothing echo'ed before your header()'s.

According to the error you posted, something has already been outputted on line 7, and your error is occuring on line 157, so just check those lines and try to understand what is going on there.

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #12 on: August 01, 2007, 01:58:21 PM »
Ah yes
Thanks guys :]

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #13 on: August 05, 2007, 02:11:06 PM »
Ok so far I have this
I have 1 page for the form and 1 page for the php like register.php and regprocess.php just to seperate it easierly
I changed the space style to fantasy/medieval, I can do more with that sort of style and its fun
I'm a bit stuck in some areas!! :[

My directory is called /test/ for now until It's actually functional and my database is auburnf_lp9sc so I have all that correct

Register.php
Code: [Select]
<table widht="550px" height="110px" align="center" style="font-family:Verdana, Arial;">
<tr align="center"><td><h3>Registration</h3></td></tr>
<form method="post" action="regprocess.php">
<tr align="Center"><td>Username: <input type="Text" name="username"></td></tr>
<tr align="Center"><td>Password: <input type="text" name="password"></td></tr>
<tr align="center"><td>E-Mail: <input type="text" name="email"></td></tr>
<tr align="center"><td>Confirm E-Mail: <input type="text" name="email1"></td></tr>
<tr align="center"><td><input type="submit" value="Register" name="register"></td></tr>
</form>
</table>
Confirm email is just so I can ban emails for now which I'll do later, soon they will have to confirm with a verification code
No problems with this part


Regprocess.php with all the registration info (hidden my database details for safety ;p)
Code: [Select]
<?php
//Include script
include('includes/functions.php');
//If set register
if(isset($_POST['register']))
{
//Variables from form
$username $_POST['username'];
$password $_POST['password'];
//hash password to hide
$password md5($password);
//Email forms
$email $_POST['email'];
$email1 $_POST['email1'];
//Date stat
$date date("F j");
//Error on filling in form
$errors 0;
$emessage "<center><font face=\"Verdana\">Sorry, we enounctered the following errors when processing your registration:<br /><ul>";
//If it is empty
if(empty($username) || empty($password) || empty($email) || empty($email1))
{
 
$errors 1;
 
$emessage .= "<li>Sorry, you must fill in <strong>all</strong> fields.  Please go back and try again.</li>";
}
if(
strlen($_POST['password']) <= 3)
{
 
$errors 1;
 
$emessage .= "<li>Sorry, your password must be <strong>atleast</strong> four characters.</li>";
}
if(
$email != $email1)
{
 
$errors 1;
 
$emessage .= "<li>Sorry, The confirmation email does not match your email.</li>";
}
//if they made errors, show them
if($errors != 0)
{
 
$emessage .= "</font></center>";
 die(
"$emessage");
}


//connect to database
mysql_connect("localhost""*************""******") or die(mysql_error());
//Database name
mysql_select_db("auburnf_lp9sc") or die(mysql_error());
//Give starting resources etc
  
$now date("U");
  
$sql "INSERT INTO gg_game(username,gil,steel,iron,last_tick,ticks,explorer,swordsman,copper,bronze) 
  VALUES(LAST_INSERTED_ID(),'10000','50','100','
$now','100','10','5','250','500')";
//perform query, user submitted data
mysql_query("INSERT INTO gg_game (username, password, email, date, gil, bronze, copper, iron, steel, explorer, swordsman) VALUES ('$username', '$password', '$email', '$date', '$gil', '$bronze', '$copper', '$iron', '$steel', '$explorer', '$swordsman')") or die(mysql_error());
//give them a nice pat on the back for doing everything just right
echo("<center><font face=\"Verdana\">Thanks for registering.  You may now log in by clicking <a href=\"/test/login.php\">here</a>.</font></center>");
}

?>
Big problem with this x.x
The
Code: [Select]
//Give starting resources etc
  $now = date("U");
  $sql = "INSERT INTO gg_game(username,gil,steel,iron,last_tick,ticks,explorer,swordsman,copper,bronze)
  VALUES(LAST_INSERTED_ID(),'10000','50','100','$now','100','10','5','250','500')";
//perform query, user submitted data
mysql_query("INSERT INTO gg_game (username, password, email, date, gil, bronze, copper, iron, steel, explorer, swordsman) VALUES ('$username', '$password', '$email', '$date', '$gil', '$bronze', '$copper', '$iron', '$steel', '$explorer', '$swordsman')") or die(mysql_error());
part does not seem to work, I have a thing that shows you how many of each resource etc you currently have and I changed the starting resources values because I changed the things (bronze etc)
I'm not sure about performing query, was I suppose to include those? ;s I included them so I could use them on the login page to view the current amount of resources etc but it doesn't show the values that I set them to, it just shows 0 for each resource etc



This is the login.php form
Code: [Select]
<table width="300px" height="110px" align="center" style="font-family:Verdana;">
<tr align="center"><td><h3>Login</h3></td></tr>
<form action="loginprocess.php" method="post">
<tr align="center"><td>Username: <input type="text" name="username"></td></tr>
<tr align="Center"><td>Password: <input type="text" name="password"></td></tr>
<tr align="center"><td><input type="submit" value="Login" name="login"></td></tr>
</form>
</table>
Pretty simple, it works

The loginprocess.php
Code: [Select]
<?php
//start it!
session_start();
if(isset(
$_POST['login']))
{
//fill in all fields
$username $_POST['username'];
$password $_POST['password'];
$password md5($password);
//connect to DB
mysql_connect("localhost""*************""******") or die(mysql_error());
mysql_select_db("auburnf_lp9sc") or die(mysql_error());
//make sure they aren't lying about there details! :O
$res mysql_query("SELECT * FROM gg_game WHERE username='$username' AND password='$password'");
//no user with details so... :O
if(mysql_num_rows($res) == 0)
{
 
 echo(
"<center><font face=\"Verdana\">We did not find your user details, please go <a href=\"login.php\">back</a> and try again.");
}
else
{
//set the session data
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
//redirect to members page =D
header("Location: ismember.php");
}
}
?>

This all works which is good ;]



ismember.php is currently the main page for when users are logged in, its also the page that shows the users current resources etc which may be wrong ;\

Code: [Select]
<?php
//start session!
session_start();
//get the username and password from the session
$password $_SESSION['password'];
$username $_SESSION['username'];
//connect to DB
mysql_connect("localhost""************""******") or die(mysql_error());
mysql_select_db("auburnf_lp9sc") or die(mysql_error());

//search db for session user and password
$res mysql_query("SELECT * FROM gg_game WHERE username='$username' AND password='$password'") or die(mysql_error());
if(
mysql_num_rows($res) == 1)
{
 while(
$get=mysql_fetch_array($res))
 {
//show user data!
  
echo("<table align=\"center\" width=\"400px\" height=\"110px\" style=\"font-family:Verdana;\">");
  echo(
"<tr align=\"center\"><td><h3>Thanks for logging in $username</h3></td></tr>");
  echo(
'<tr align="center"><td>Your Password is: Encrypted for safety.</td></tr>');
  echo(
'<tr align="Center"><td>Your E-mail is: '$get['email'] .'</td></tr>');
  echo(
'<tr align="center"><td>You Joined: '$get['date'] .'</td></tr>');
  echo(
'<tr align="center"><td><br /><br /></td></tr>');
  echo(
'<tr align="center"><td><h3>Current Resources</td></tr>');
  echo(
'<tr align="Center"><td>Gil: '$get['gil'] .'</td></tr>');
  echo(
'<tr align="center"><td>Bronze: '$get['bronze'] .'</td></tr>');
  echo(
'<tr align="center"><td>Copper: '$get['copper'] .'</td></tr>');
  echo(
'<tr align="center"><td>Iron: '$get['iron'] .'</td></tr>');
  echo(
'<tr align="center"><td>Steel: '$get['steel'] .'</td></tr>');
  echo(
'<tr align="center"><td>Explorers: '$get['explorer'] .'</td></tr>');
  echo(
'<tr align="center"><td>Swordsman: '$get['swordsman'] .'</td></tr>');
  echo(
'<tr align="center"><td><br /><br /></td></tr>');
  echo(
'<tr align="center"><td><h3>Account Options</td></tr>');
  echo(
'<tr align="center"><td><li><a href="/test/logout.php">Logout</a></li></td></tr>');
  echo(
"</table>");
 }
 
 }
 else
 
//hmmm we didnt find there data? error time!
 
{
 echo(
"<center><font face=\"Verdana\">Sorry, you are not logged in, proceed <a href=\"/test/login.php\">here</a> to login or <a href=\"/test/register.php\">here</a> to register.");
 }

?>



Logout.php

Code: [Select]
<?php
//start it!
session_start();
//logout!
session_destroy();
echo(
"<h3><center><font face=\"Verdana\">You have sucessfully logged out.<font></center></h3><br />");
echo(
"<h4><center><font face=\"Verdana\">Proceed <a href=\"/test/login.php\">here</a> to login again.</font></center></h4>");
?>



This is pm.php which works but the user does not receive the pm? ;\

Code: [Select]
<?php 
session_start
(); 
$username $_SESSION['username']; 
$password $_SESSION['password']; 
$do $_GET['do']; 
mysql_connect("localhost""**********""******") or die(mysql_error()); 
mysql_select_db("auburnf_lp9sc") or die(mysql_error()); 
$res mysql_query("SELECT * FROM gg_game WHERE username='$username' AND password='$password'"); 
if(
mysql_num_rows($res) == 0

die(
"<center><font face=\"Verdana\">We did not find your user details, please go <a href=\"test/login.php\">back</a> and try again."); 

if(
$do == ""

echo(
'<table align="center" width="500px" height="100px"> 
      <tr align="center" bgcolor="e1e1e1"><td><a href="pm.php?do=inbox">Inbox</a></td></tr> 
      <tr align="center" bgcolor="e1e1e1"><td><a href="pm.php?do=send">Send</a></td></tr> 
      </table>'
); 
}
else if(
$do == "inbox"

    
$res mysql_query("SELECT * FROM `gg_pm` WHERE `to` = '$username' ORDER BY `id` DESC") or die(mysql_error()); 
    echo(
'<center><h3>Inbox</h3></center>'); 
    echo(
'<table align="center" width="500px" height="100px">'); 
    while(
$get mysql_fetch_array($res)) 
    { 
        
$title $get['title']; 
        
$from $get['from']; 
        
$id $get['id']; 
        echo(
'<tr align="center" bgcolor="e1e1e1"><td><a href="">'$title .'</a></td><td>From: '$from .'</td></tr>'); 
    } 
    if(
mysql_num_rows($res) == 0
    { 
        echo(
'<tr align="center" bgcolor="e1e1e1"><td>Sorry, no messages in the inbox.</td></tr>'); 
    } 
}
else if(
$do == "send"

    echo(
'<table align="center" width="500px" height="100px"> 
          <form method="post" action=""> 
          <tr align="center" bgcolor="e1e1e1"><td>To: <input type"text" name="to"></td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td>Title: <input type="text" name="title"></td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td>Message:</td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td><textarea name="message" rows="5" cols="50"></textarea></td></tr> 
          <tr align="Center" bgcolor="e1e1e1"><td><input type="submit" value="send" name="submit"></td></tr> 
          </form> 
          </table>'
); 

else if(
$do == "sendit"

    
$to $_POST['to']; 
    
$title $_POST['title']; 
    
$message $_POST['message']; 
    
$from $username
    
mysql_query("INSERT INTO `gg_pm` (`to`, `title`, `message`, `from`) VALUES ('$to', '$title', '$message', '$from')") or die(mysql_error()); 
    echo(
"Message sent to $to.  Thanks.  Click <a href=\"test/pm.php\">here</a> to goto your main PM box."); 
}
else if(
$do == "view"

    
$id $_GET['id']; 
    
$mes mysql_query("SELECT * FROM gg_pm WHERE `id`= '$id'") or die(mysql_error()); 
    echo(
'<table width="500px" height="100px" align="Center">'); 
    while(
$getit mysql_fetch_array($mes)) 
    { 
        
$from $getit['from']; 
        
$message $getit['message']; 
        echo(
'<tr align="center" bgcolor="e1e1e1"><td>From:  '$from .'</td></tr> 
              <tr align="Center" bgcolor="e1e1e1"><td>Message:</td></tr> 
              <tr align="center" bgcolor="e1e1e1"><td><textarea rows="5" cols="50">'
$message .'</textarea></td></tr> 
              <tr align="center" bgcolor="e1e1e1"><td><a href="?do=reply&id='
$id .'">Reply</a></td></tr> 
              <tr align="center" bgcolor="e1e1e1"><td><a href="?do=delete&id='
$id .'">Delete</a></td></tr> 
              <tr align="center" bgcolor="e1e1e1"><td><a href="?do=">PM Home</a> | <a href="/logsys/ismember.php">UserCP Home</a></td></tr>'
); 
              
     
    }          
    echo(
'</table>'); 
}
else if(
$do == "reply"

    
$id $_GET['id']; 
    
$getname mysql_query("SELECT * FROM `gg_pm` WHERE `id` = '$id'"); 
    while(
$gname mysql_fetch_array($getname)) 
    { 
    
$to $gname['to']; 
    
$title $gname['title']; 
    echo(
'<form method="post" action="?do=sendit"> 
          <table width="500px" height="100px" align="Center"> 
          <tr align="center" bgcolor="e1e1e1"><td>To: <input type"text" name="to" value="'
$to .'"></td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td>Title: <input type="text" name="title" value="Re: '
$title .'"></td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td>Message:</td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td><textarea name="message" rows="5" cols="50"></textarea></td></tr> 
          <tr align="Center" bgcolor="e1e1e1"><td><input type="submit" value="send" name="submit"></td></tr> 
          </form>'
); 
    } 
}
else if(
$do == "delete"

    
$id $_GET['id']; 
    
mysql_query("DELETE FROM `gg_pm` WHERE `id` = '$id' AND `to` = '$username'") or die(mysql_error()); 
    echo(
"<center>Message deleted.  Goto your <a href=\"?do=\">PM home</a> or your <a href=\"?do=inbox\">inbox</a>."); 

?>



This is the buy.php where you buy units, only 2 for now until it actually works

Code: [Select]
<table widht="550px" height="110px" align="center" style="font-family:Verdana, Arial;">
<tr align="center"><td width="272"><h3>buy</h3></td></tr>
<form method="post" action="buyprocess.php">
<tr align="Center"><td>explorer<input type="text" name="explorer">
10gil</td>
</tr>
<tr align="Center"><td>swordsman<input type="text" name="swordsman">
20gil</td>
</tr>
<tr align="center"><td><input type="submit" value="buy" name="buy"></td></tr>
</form>
</table>
<br />
<table widht="550px" height="110px" align="center" style="font-family:Verdana, Arial;">
<tr align="center"><td width="272"><h3>sell</h3></td></tr>
<form method="post" action="buyprocess.php">
<tr align="Center"><td>explorer<input type="text" name="explorer">
5gil</td>
</tr>
<tr align="Center"><td>swordsman<input type="text" name="swordsman">
10gil</td>
</tr>
<tr align="center"><td><input type="submit" value="buy" name="buy"></td></tr>
</form>
</table>






This is buyprocess.php which is seriously not working x.x

Code: [Select]
<?php
//Include script
include('includes/functions.php');
//If set register
if(isset($_POST['buy']))
{
//Variables from form
$explorer $_POST['explorer'];
$swordsman $_POST['swordsman'];
$explorerbuy $_POST['explorer' 10];
$swordsmanbuy $_POST['swordsman' 20];
//buytotal
$buytotal $_POST['explorerbuy' 'swordsmanbuy'];
//Buy units
$buyunits $gilvalue $buytotal
//Error on filling in form
$errors 0;
$emessage "<center><font face=\"Verdana\">Sorry, we enounctered the following errors when processing your purchase.<br /><ul>";
//If it is empty
if(empty($explorer) || empty($swordsman))
{
 
$errors 1;
 
$emessage .= "<li>Sorry, you must fill in <strong>all</strong> fields.  Please go back and try again. If you do not wish to buy one type of unit fill in the field with 0.</li>";
}
if(
strlen($_POST['explorer,swordsman']) <= -1)
{
 
$errors 1;
 
$emessage .= "<li>You cannot purchase negative units!.</li>";
}
if(
strlen($_POST['explorer,swordsman']) >= 9999999999)
{
 
$errors 1;
 
$emessage .= "<li>You cannot purchase too many units!.</li>";
}
if(
$buytotal $gilvalue)
{
 
$errors 1;
 
$emessage .= "<li>Sorry you do not have enough gil for that many units.</li>";
}
//if they made errors, show them
if($errors != 0)
{
 
$emessage .= "</font></center>";
 die(
"$emessage");
}
//Buy units now
$explorernew $explorerbuy
$swordsmannew 
$swordsmanbuy
if($buytotal $gilvalue)
{
//connect to database
mysql_connect("localhost""***********""*****") or die(mysql_error());
//Database name
mysql_select_db("auburnf_lp9sc") or die(mysql_error());
//Givename
$gilvalue mysql_query("SELECT * FROM gg_game WHERE gil='$gil'");
$explorervalue mysql_query("SELECT * FROM gg_game WHERE explorer='$explorer'");
$swordsman mysql_query("SELECT * FROM gg_game WHERE swordsman='$swordsman'");
//Give starting resources etc
  
$now date("U");
  
$sql "INSERT INTO gg_game(username,gil,steel,iron,last_tick,ticks,explorer,swordsman,copper,bronze) 
  VALUES(LAST_INSERTED_ID(),'10000','50','100','
$now','100','$explorernew','$swordsmannew','250','500')";
//perform query, user submitted data
mysql_query("INSERT INTO gg_game (username, password, email, date, gil, bronze, copper, iron, steel, explorer, swordsman) VALUES ('$username', '$password', '$email', '$date', '$gil', '$bronze', '$copper', '$iron', '$steel', '$explorer', '$swordsman')") or die(mysql_error());
} else {
 
$errors 1;
 
$emessage .= "<li>Sorry you do not have enough gil for that many units.</li>";
 }

?>


Please help me with the
Code: [Select]
//Give starting resources etc
  $now = date("U");
  $sql = "INSERT INTO gg_game(username,gil,steel,iron,last_tick,ticks,explorer,swordsman,copper,bronze)
  VALUES(LAST_INSERTED_ID(),'10000','50','100','$now','100','$explorernew','$swordsmannew','250','500')";
//perform query, user submitted data
mysql_query("INSERT INTO gg_game (username, password, email, date, gil, bronze, copper, iron, steel, explorer, swordsman) VALUES ('$username', '$password', '$email', '$date', '$gil', '$bronze', '$copper', '$iron', '$steel', '$explorer', '$swordsman')") or die(mysql_error());
trouble and the buy page and pm page trouble :[



I've been working on quite a few tutorials lately and giving myself comments for what things mean in a php script like to test myself. I've noticed that PHP isn't really huge in different commands needed for certain things, for most things that I've been on I havn't learnt many new commands, is there really that much to learn? Because it seems that most things you can make with PHP can be made using the same commands. ;o

I'm stil a bit confused about errors from MySQL query or something
It tells me that on a certain line an unexpected $END is there, when I delete it I end up with another unexpected one and even more when I delete the script and soon end up with just a MySQL query which is causing the problem
and sometimes when I do 2 $variable things like
$hi = [.'hi'.]
$hello = 1
or something like that I get the same unexpected $END error ;\ and it just keeps telling me that its all unexpected, I think I'm missing something important when that happens ;\
« Last Edit: August 05, 2007, 02:27:38 PM by Lp9Sc »

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #14 on: August 05, 2007, 03:00:54 PM »
Here are a few tips in general to prevent errors, it's not specific to your problems:

Make sure there is a semi-colon at the end of every statement. Such as your $hello = 1, make sure it's $hello = 1; (<----- note the semi-colon :P)
In your SQL query inserts, make sure that the number of values you insert is the same as the number of columns there are in the table.


Quote
I've noticed that PHP isn't really huge in different commands needed for certain things, for most things that I've been on I havn't learnt many new commands, is there really that much to learn? Because it seems that most things you can make with PHP can be made using the same commands. ;o
Well, yeah, PHP is pretty easy once you understand how it works. Generally, there are a few useful functions that you'll get to know as you practice using PHP, but it's not really that much to remember. And if you can't remember it, there's always the PHP manual :)


Here's something that I noticed from your scripts: All your database connections are hard-coded onto each page. I think it's more useful to include() all your common code so that you can just change it in one file and the whole website is updated. Just put your db connection code into a file such as 'config.php', and use include("config.php"); at the top of each page.


As for your problem with giving the resources, I'm not sure what's wrong. The values you enter correspond with the columns in the table, but it looks like you have the same query twice. $sql first, then the actual mysql query. I'm not sure what that's about, because $sql isn't actually executed, you just left it there.
I can't find anything wrong with the query, except maybe you could try taking out the apostrophes on the values. Integers (numbers) don't need apostrophes, but strings (text) do. So, '$username' should work, but '$gil' might not. I'm not exactly sure that's the problem though, maybe using apostrophes is fine.
To help us find the error, could you post the exact error? Just copy and paste it here :)


You haven't actually said what's wrong with buyprocess.php, so there could be deeper problems, but I found some syntax errors: you forgot to add a semi-colon to several statements:
Code: [Select]
$buyunits = $gilvalue - $buytotal
$explorernew = $explorerbuy
$swordsmannew = $swordsmanbuy
Just add a semi-colon to the end of those lines, and try the page again. If it doesn't work, see where else you missed a semi-colon.


As for pm.php, it may be a problem with your INSERT query like in regprocess.php. It helps to show the error that was produced.

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #15 on: August 05, 2007, 03:06:02 PM »
Ok, I have to answer this piece by piece as it is a long post :)

I'll start from the top

Code: [Select]
//Give starting resources etc
  $now = date("U");
  $sql = "INSERT INTO gg_game(username,gil,steel,iron,last_tick,ticks,explorer,swordsman,copper,bronze)
  VALUES(LAST_INSERTED_ID(),'10000','50','100','$now','100','10','5','250','500')";
//perform query, user submitted data
mysql_query("INSERT INTO gg_game (username, password, email, date, gil, bronze, copper, iron, steel, explorer, swordsman) VALUES ('$username', '$password', '$email', '$date', '$gil', '$bronze', '$copper', '$iron', '$steel', '$explorer', '$swordsman')") or die(mysql_error());

1. The $sql text redundant.
2. The $sql text is buggy
here's a fix:
Code: [Select]
<?php
  $now 
date("U");
  
$sql "INSERT INTO gg_game (username, password, email, date, gil, bronze, copper, iron, steel, explorer, swordsman) VALUES ('$username', '$password', '$email', '$date', '$gil', '$bronze', '$copper', '$iron', '$steel', '$explorer', '$swordsman')";
  
mysql_query($sql) or die(mysql_error());
?>


Your problems were that you copied too much from my original post. LAST_INSERTED_ID() can only be used if a mysql-statement with an auto ID has already been use.



Quote
I'm not sure about performing query, was I suppose to include those? ;s I included them so I could use them on the login page to view the current amount of resources etc but it doesn't show the values that I set them to, it just shows 0 for each resource etc

I don't understand what you mean... =/



The problem with your pm.php file and the user not recieveing any pms is simple:
Your code has a <form>-tag in it, but inside this <form>-tag there is no action declared. This should be "pm.php?do=sendit".

Code: [Select]
<?php
else if($do == "send"

    echo(
'<table align="center" width="500px" height="100px"> 
          <form method="post" action="pm.php?do=sendit"> 
          <tr align="center" bgcolor="e1e1e1"><td>To: <input type"text" name="to"></td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td>Title: <input type="text" name="title"></td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td>Message:</td></tr> 
          <tr align="center" bgcolor="e1e1e1"><td><textarea name="message" rows="5" cols="50"></textarea></td></tr> 
          <tr align="Center" bgcolor="e1e1e1"><td><input type="submit" value="send" name="submit"></td></tr> 
          </form> 
          </table>'
); 

else if(
$do == "sendit" AND isset($_POST['submit'])) 

    
$to $_POST['to']; 
    
$title $_POST['title']; 
    
$message $_POST['message']; 
    
$from $username
    
mysql_query("INSERT INTO `gg_pm` (`to`, `title`, `message`, `from`) VALUES ('$to', '$title', '$message', '$from')") or die(mysql_error()); 
    echo(
"Message sent to $to.  Thanks.  Click <a href=\"test/pm.php\">here</a> to goto your main PM box."); 
}
?>


I added an isset($_POST['submit']) statement in the boolean. This simply checks if the user actually sent something from the $do=send form. You should also add in a filter to check if all fields were actually set. You did this quite well in your regprocess.php file.



Alright, concerning buy.php.

I seriously doubt that what you did will work the way you wanted it to.
Code: [Select]
<?php
if(isset($_POST['buy']))
{
//Variables from form
$explorer $_POST['explorer'];
$swordsman $_POST['swordsman'];
$explorerbuy $_POST['explorer']*10;
$swordsmanbuy $_POST['swordsman']*10;
//buytotal
$buytotal $explorerbuy $swordsmanbuy;
//Buy units
$buyunits $gilvalue $buytotal
//Error on filling in form
...
?>

$_POST['x'] only stored the variable with name x. To perform an action with this variably, you don't have to change the name, but the whole variable itself. So if you want to multiplay x by ten its $x=$_POST['x']*10;. This should be fixed in the code above.

Then you need to read this link to help you with your queries. I'm sorry, but I don't have enough time to explain it to you right now. Hopefully someone else will have enough time. Sorry

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #16 on: August 06, 2007, 05:19:18 PM »
Ok, thanks. Most things are working now ^^
Something that fixed many problems was the lack of } xD So I add a few more till it said unexpected } then I deleted that one so It's all ok

Umm, how do I get starting resources etc if I can't use (LAST_INSERTED_ID() ?

before registration?

I saw a tutorial on how to set up a buy form etc and finally done that one xd
I added quite a few units to buy etc so it got up to around 500 lines long

But I can't buy the units, when I click buy on the form it just sort of refreshes the page

Anyway, main thing I would like to know now is how to set starting resources? x.x Please help :]


The include(includes/functions.php) which config.php was included on to got annoying and mostly didn't actually login to my database so I'm just using the database login for every php page now instead of including functions to include config or maybe later I'll try just including config instead of having functions
« Last Edit: August 06, 2007, 05:21:18 PM by Lp9Sc »

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #17 on: August 07, 2007, 03:06:53 AM »
Ah, you mean you don't know how to get the user ID when the user has logged in?
I think you should read some articles/tutorials about user authentication first.

Basically, after the user has logged in, you store the user's ID in a session.
Then in your SQL queries, if you want to update the user, you just add 'where `id`=$_SESSION[id]' to the end of the query, and it will only update for the logged in user.

Here's a few example queries in case you don't get it yet:
Code: [Select]
//This one adds 1000 gil to the logged in player
mysql_query("update `players` set `gil`=gil+1000 where `id`=$_SESSION[id]");

//This one gets all the data about a user
mysql_query("select * from `players` where `id`=$_SESSION[id]");


I see that you have a similar process in loginprocess.php, so on the login page, set $_SESSION['id'] to $get['id']. ($get contains all the user info, it's in loginprocess.php and ismember.php)

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #18 on: August 07, 2007, 07:27:50 AM »
Ok thanks :]
So that should give the new user starting resources ya?

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #19 on: August 07, 2007, 07:45:56 AM »
Yep, so long as you select the correct row from the database. I'm assuming that each player entry has an `id` column that is unique for each player. Then you can select a player's data so long as you know their ID.
You can't use LAST_INSERTED_ID() because it gives the ID of the last inserted row from that page. So if you just added an item to the items table, then it will give the ID of that new row.
And if no inserts have been made yet on that page, then I don't think you will even get an ID returned.
« Last Edit: August 07, 2007, 07:48:12 AM by Zeggy »

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #20 on: August 08, 2007, 06:15:37 AM »
I have to wait for my webmaster to look at my script to fix a few things that I can't be bothered to do
So while I'm waiting I've been making some flash projects again
I've made a simple PONG type game which you have to beat he comp at xd with 3 different challenge modes and I've made a chat room which is probably the mot difficult thing I have ever made x.x Trial and Error! But finally it actually works

Now I'm just wonder.. "Make an online flash game?"
It's probably going to be a space game deathmatch sort of thing
« Last Edit: August 08, 2007, 06:17:33 AM by Lp9Sc »

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #21 on: August 08, 2007, 06:40:40 AM »
You want to make a flash game played online with other players?
That must be difficult :)
Single player games are already hard to make :P

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #22 on: August 08, 2007, 07:12:39 AM »
Nah I think single player is so easy and its pretty fun
The first thing I knew about flash was the basics of making graphics, tweaning and making things move
Then I found the basics of making a game, the first thing I new about that was moving left, up, down, right then I found by myself that you can change the speed of those and then you can also apply a speed increase for how long you hold the button pressed and you can also increase the max speed of the object by holding 2 buttons xd

Online flash is confusing ;o
Webmaster fixed some errors now so I'm gonna stop the flash for a bit xd

Anyway

Going back to what you previously said about

//This one adds 1000 gil to the logged in player
mysql_query("update `players` set `gil`=gil+1000 where `id`=$_SESSION[id]");

//This one gets all the data about a user
mysql_query("select * from `players` where `id`=$_SESSION[id]");


I don't think it works still =\ I register, login and on  the member page it should show how much gil I currently have but it just shows 0

The regprocess page as
Code: [Select]
mysql_query("update `gg_game` set `gil`=gil+10000 where `id`=$_SESSION[id]");
and the member page has
Code: [Select]
$password = $_SESSION['password'];
$username = $_SESSION['username'];
$gil = $_SESSION['gil'];
Code: [Select]
$res = mysql_query("SELECT * FROM gg_game WHERE username='$username' AND password='$password' AND gil='$gil'") or die(mysql_error());
if(mysql_num_rows($res) == 1)
{
 while($get=mysql_fetch_array($res))
 {
  echo("<table align=\"center\" width=\"400px\" height=\"110px\" style=\"font-family:Verdana;\">");
  echo("<tr align=\"center\"><td><h3>Currently logged in as: $username</h3></td></tr>");
  echo("<tr align=\"center\"><td><h3>Gil: $gil</h3></td></tr>");
  echo('<tr align="Center"><td>E-mail: '. $get['email'] .'</td></tr>');
  echo('<tr align="center"><td>Joined: '. $get['date'] .'</td></tr>');
  echo('<tr align="center"><td>Gil: '. $get['gil'] .'</td></tr>');
  echo("</table>");
(just highlighting the important bits)

I think I've done the MySQL tables wrong, I'm no good at doing the data types =\

The member page shows

Currently logged in as: regreg
Gil: 
E-mail: regreg@regreg.com
Joined: August 8
Gil: 0

So email, joined date and username is done right, password isn't suppose to show which is good
Its just Gil
I tried both ways (get gil and $gil)
The $gil way shows nothing and the get gil way shows 0


Mysql gg_game table
id int 15  NOTNULL
username varchar 255 NOTNULL
password varchar 255 NOTNULL

is id wrong? ;o

primary key = username

I tried making id primary key too but it still doesn't help =\
« Last Edit: August 08, 2007, 07:33:30 AM by Lp9Sc »

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #23 on: August 11, 2007, 06:39:37 PM »
Code: [Select]
<?php
//This one gets all the data about a user
mysql_query("select * from `players` where `id`=$_SESSION[id]");
?>


Code: [Select]
<?php
$password 
$_SESSION['password'];
$username $_SESSION['username'];
$gil $_SESSION['gil'];
?>
Ok, first off: to actually get the values from the mysql-query you need to pull the data from the first query into an array.
Furthermore, every session you use needs to be declared somewhere.
What I'm saying is that you're $gil-statement is incomplete.

Code: [Select]
<?php
$sql 
"select * from `players` where `id`=$_SESSION[id]";
$rs mysql_query($sql); //Step 1
$rs mysql_fetch_array($rs); //Step 2
?>


The above code took your sql-statement from before and executed it in Step 1. Now to access the data it was pulled into an array in step 2. Now, (important), all the data from the players table where the id is the one from the session is stored in the array $rs.
Here are some examples of what I mean:
Code: [Select]
echo $rs['id']; // Displays the id of the currently logged in user
echo $rs['name']; // Displays the name of the currently logged in user

So, to display the $gil you need to do the following:
Code: [Select]
<?php
$password 
$_SESSION['password'];
$username $_SESSION['username'];

$sql "SELECT * FROM gg_game,players 
    WHERE gg_game.user_id=players.id 
    AND players.name='
$username
    AND players.password='
$password'";
$rs mysql_query($rs);
$rs mysql_fetch_array($rs);

$gil $rs['gil'];
?>


Hmm.. can someone double check this query for me. It's late and I haven't touched PHP in a week --;

Quote
Mysql gg_game table
id int 15  NOTNULL
username varchar 255 NOTNULL
password varchar 255 NOTNULL

is id wrong? ;o

primary key = username

I tried making id primary key too but it still doesn't help =\
This should help you:
id should be the only primary key.
id should also be auto_incremented.

Offline Lp9Sc

  • Level 5
  • *
  • Posts: 15
  • Reputation: +1/-0
    • View Profile
Re: BBG using PHP + MySql Help? ;D
« Reply #24 on: August 12, 2007, 05:37:48 AM »
Oh thanks ;D
I thought I had to do the MySQL query  for every single resource ;o but I can just use $rs? Yay

I've been lately working on a flash game with my friend (hes teaching me alot of what I don't know about ;o)

Oh by the way, would I need cron job to do ticks? Or can I set up a scripted timer? How would I do either of them? o.o

I've learnt alot from you guys ^^

EDIT
Nope that still doesn't seem to be working ;o

and in member page I get error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/auburnf/public_html/lp9sc/test/ismember.php on line 30

Also I am unable to login

EDIT
----
Ok...
My flash game has the Register/Login complete now (I forgot something stupid x.x)
And my BBG has Register/Login done
Thats all I have so far for now ^^
I've been working on flash mainly lately because I keep getting problems with the BBG
« Last Edit: August 13, 2007, 05:13:02 PM by Lp9Sc »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal