Amount not showing up on site..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Amount not showing up on site..

Post by Seifer »

I am attempting to make a site where it is a website-game, and the gold after you log in won't show up...unless I run it to update through the login process, but it won't update the amount until they log in again. Therefore I need to add the script into the index page to auto update the gold as it increases and decreases. Here is my code:

Code: Select all

<?php session_start(); ?>
<html>
<head>
<title>.: Test Document :..: Home :.</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$dbname = "random";
$dbpass = "random";
$db = "random";
$link = mysql_connect(localhost, $dbname, $dbpass);
$sdb = mysql_select_db($db, $link);
$result = mysql_query("SELECT uid FROM users WHERE uname = $_SESSION&#1111;'username']");
$row = mysql_fetch_array($result);
$uid = $row&#1111;'0'];
$mgold = mysql_query("SELECT gold FROM user_info WHERE uid = $uid");
$gold = mysql_fetch_array($mgold);
$_SESSION&#1111;'gold'] = $gold&#1111;'0'];
$final = $gold&#1111;'0'];
?>
<style type="text/css">
body&#123;background-color:"#626262";color:"#CCCCCC";font-style:"arial";size:"10";&#125;
</style>

<br>
<br>
<br>
<div align="center"> </div>
<table width="978" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td height="100" colspan="4" valign="top"><center>
        <img src="images/title.jpg" width="400" height="100" border="0" usemap="#Map3"> 
      </center></td>
    <td width="1"></td>
  </tr>
  <tr> 
    <td width="150" rowspan="2" valign="top"><p><img src="images/register.jpg" width="150" height="50" border="0" usemap="#Map"><img src="images/login.jpg" width="150" height="50" border="0" usemap="#Map2"> 
      </p></td>
    <td height="100" colspan="3" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td></td>
  </tr>
  <tr> 
    <td width="100" rowspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td width="477" rowspan="2" valign="top"><div align="center"> 
        <p>Scripting Phase.. (10-15-2003)<br>
          By Mooksman</p>
        <p align="left">Scripting has just started, as you can see the site is 
          quite bare. We will be scripting the simple stuff first, as in login, 
          register, etc. until the mySQL databases are properly fit to suit my 
          wants..er, needs :). </p>
        <p align="left">Thanks,<br>
          Admin Mooksman <Webmaster></p>
        <p>&nbsp;</p>
      </div></td>
    <td width="250" rowspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td height="180"></td>
  </tr>
  <tr> 
    <td height="29" align="center" valign="middle"> 
      <div align="center"><font size="-1">Currently logged in as:<br>
        <font color="#00CC00"><?php echo $_SESSION&#1111;'username']; ?></font><br>
        <font size="-1">Gold: <font color="#00CC00"><?php echo "$final"; ?></font></div></td>
    <td></td>
  </tr>
</table>
<map name="Map">
  <area shape="rect" coords="2,1,149,49" href="/register.php" alt="Register">
  <area shape="rect" coords="3,49,146,97" href="/register.php">
</map>
<map name="Map2">
  <area shape="rect" coords="1,1,152,51" href="/login.php" alt="Login">
</map>
<map name="Map3">
  <area shape="rect" coords="1,2,399,99" href="#">
</map>
</body>
</html>
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

also seifer.travisbsd.org/ if you want to check it out, only the login works, username/password: guest/guest. There should be 100 gold apearing on the bottom left corner...Under Username.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It looks like you get the gold value from the db everytime the page loads, and i don't see where you save the gold back to the db?
Presuming you want to get their gold value from the database when they login, but after that always use the session gold value then you probably want something like :

Code: Select all

if(!isset($_SESSION['gold'])){
    //connect to db and get gold value
    $_SESSION['gold'] = $gold[0];
}
$final = $_SESSION['gold'];
...
I'm not sure how $final differs from $_SESSION['gold'] by looking at your code so you may not even need the $final = bit .. just stick with $_SESSION.
Then you just add values to $_SESSION['gold'] until at some point you'll want to write $_SESSION['gold'] back into the db.
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

No, because in the game I am creating the gold is going to have to be updated every time they visit a page, as they could find random amounts, kill things to give them gold, etc. I have seifer.travisbsd.org/test.php that adds gold to it, then go back to the main page and hit refresh..the added 500 gold doesn't show up.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try using $uid = $row['uid']; instead of $uid = $row['0'];
and $_SESSION['gold'] = $gold['gold']; $final = $gold['gold']; too.

Also put error_reporting(E_ALL); right after <?php
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

I did that all, I need it to work without the (!isset) part, as that won't allow me to reach what I want it to do.
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

Oh, forgot to mention, I did the ['gold'] and ['uid'], it still doesn't work.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It's hard to see exactly what's going on without seeing the code for the 3 relevant pages (main, test and freegold). The chunk of code above is the main page by the look of it, but that doesn't appear to save any gold values, so i presume test and freegold do that? Also, as you are reading/writing to the db on every page hit..where does the need for the session come into it?

Maybe create .phps files for those 3, might help clear my confusion ;)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

oh..and you should change
SELECT uid FROM users WHERE uname = $_SESSION['username']
to
SELECT uid FROM users WHERE uname = '{$_SESSION['username']}'
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

That worked :). What does the brackets on the end do really? I never have used, nor thought of using, them before.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, without them the query evaluates to
SELECT uid FROM users WHERE uname = guest
and it will look for a column called guest in the db.
So normally you'de quote the var , like uname='guest' but as you want to use $_SESSION['username'] obviously '$_SESSION['username']' won't work as PHP will bork on the single quotes.
So to cut a long story short '{$_SESSION['username']}' evaluates to 'guest'
(the {}'s allow the internal 's to parse ok)
:o
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

Oh okay, thanks a ton :). Now my CSS isn't working for a couple of people, could you goto the my site and tell me what color the font is in the center of the main page..It is set to be #CCCCCC but he said it is black..
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

yup, it's black. But it's because of the quotes in your text/css bit..i.e change it to
<style type="text/css">
body{background-color:#626262;color:#CCCCCC;font-style:arial;font-size:10;}
td{color:#CCCCCC;font-style:arial;font-size:10;}
</style>
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

Okay, done.
Post Reply