News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Linking queries to smf database per user

Started by brion, November 09, 2009, 02:42:07 PM

Previous topic - Next topic

brion

Here is the top of the file

<?php require("SSI.php");
mysql_connect('localhost''user''pass') or die(mysql_error());
mysql_select_db('database_name') or die(mysql_error());

if (isset(
$_GET["x"])) {
$x explode(":",$_GET["x"]);

    switch(
$x[0])
{
case 'area1':
area1();
break;

case 'area2':
area2();
break;

case 'area3':
area3();
break;

 }
}
 else { 
main(); }
?>


I removed the user/pass/db_name for safety. Here is the main function part which is suppose to show the amount of items in their account.

<?php
function main() {

echo 
'<table align="right"><tr><td>';
ssi_welcome();
echo 
'</td></tr></table>';
echo 
'<h2>Metal Detecting Game.</h2>';

$checkk db_query("SELECT clad FROM smf_members WHERE ID_MEMBER='{$user_info['id']}'"__FILE____LINE__);
$check2 mysql_fetch_array($checkk);

echo 
'<table width="20%" align="right">';
echo 
"<tr><td><b>Clad:</b> ".$check2['clad']."</td></tr>";
echo 
"<tr><td><b>Silver Coins:</b> 0</td></tr>";
echo 
'<tr><td><b>Relics:</b> 0</td></tr>';
echo 
'</table><br />';
echo 
'<center><img src="/game/park1.jpg" usemap="#park1" border="0">

<map name="park1">
<area shape="rect" coords="413,317,477,354" href="game.php?x=area1" target="_SELF">
<area shape="rect" coords="334,310,395,354" href="game.php?x=area2" target="_SELF">
<area shape="rect" coords="233,305,304,351" href="game.php?x=area3" target="_SELF">
<area shape="rect" coords="174,302,196,351" href="game.php?x=area4" target="_SELF">
<area shape="rect" coords="88,324,153,340" href="game.php?x=area5" target="_SELF">
<area shape="rect" coords="8,311,73,348" href="game.php?x=area6" target="_SELF">
<area shape="rect" coords="26,233,54,256" href="game.php?x=area7" target="_SELF">
<area shape="rect" coords="112,248,147,280" href="game.php?x=area8" target="_SELF">
<area shape="rect" coords="199,237,242,285" href="game.php?x=area9" target="_SELF">
<area shape="rect" coords="285,233,310,248" href="game.php?x=area10" target="_SELF">
<area shape="rect" coords="334,246,391,288" href="game.php?x=area11" target="_SELF">
<area shape="rect" coords="417,250,466,279" href="game.php?x=area12" target="_SELF">
<area shape="rect" coords="14,208,63,217" href="game.php?x=area13" target="_SELF">
<area shape="rect" coords="97,205,76,190" href="game.php?x=area14" target="_SELF">
<area shape="rect" coords="124,167,176,182" href="game.php?x=area15" target="_SELF">
<area shape="rect" coords="122,196,146,209" href="game.php?x=area16" target="_SELF">
<area shape="rect" coords="168,193,193,208" href="game.php?x=area17" target="_SELF">
<area shape="rect" coords="225,199,251,214" href="game.php?x=area18" target="_SELF">
<area shape="rect" coords="307,203,328,222" href="game.php?x=area19" target="_SELF">
<area shape="rect" coords="411,204,449,233" href="game.php?x=area20" target="_SELF">

</map>
</center><br /><br />'
;

echo 
'<center><a href="index.php">Back to Forum</a></center>';
}


And here is one of the things when you find an item, it's suppose to add +1 to the clad field of that users account.

function area2() {
echo '<center><b>You found a memorial penny!</b><br /><br /><img src="/game/cladpenny.jpg"></center>';
db_query("UPDATE smf_members SET clad=clad+1 WHERE ID_MEMBER='{$user_info['id']}'", __FILE__, __LINE__) or die( mysql_error());
echo '<center><img src="/game/park1.jpg" usemap="#park1" border="0">

<map name="park1">
<area shape="rect" coords="413,317,477,354" href="game.php?x=area1" target="_SELF">
<area shape="rect" coords="334,310,395,354" href="game.php?x=area2" target="_SELF">
<area shape="rect" coords="233,305,304,351" href="game.php?x=area3" target="_SELF">
<area shape="rect" coords="174,302,196,351" href="game.php?x=area4" target="_SELF">
<area shape="rect" coords="88,324,153,340" href="game.php?x=area5" target="_SELF">
<area shape="rect" coords="8,311,73,348" href="game.php?x=area6" target="_SELF">
<area shape="rect" coords="26,233,54,256" href="game.php?x=area7" target="_SELF">
<area shape="rect" coords="112,248,147,280" href="game.php?x=area8" target="_SELF">
<area shape="rect" coords="199,237,242,285" href="game.php?x=area9" target="_SELF">
<area shape="rect" coords="285,233,310,248" href="game.php?x=area10" target="_SELF">
<area shape="rect" coords="334,246,391,288" href="game.php?x=area11" target="_SELF">
<area shape="rect" coords="417,250,466,279" href="game.php?x=area12" target="_SELF">
<area shape="rect" coords="14,208,63,217" href="game.php?x=area13" target="_SELF">
<area shape="rect" coords="97,205,76,190" href="game.php?x=area14" target="_SELF">
<area shape="rect" coords="124,167,176,182" href="game.php?x=area15" target="_SELF">
<area shape="rect" coords="122,196,146,209" href="game.php?x=area16" target="_SELF">
<area shape="rect" coords="168,193,193,208" href="game.php?x=area17" target="_SELF">
<area shape="rect" coords="225,199,251,214" href="game.php?x=area18" target="_SELF">
<area shape="rect" coords="307,203,328,222" href="game.php?x=area19" target="_SELF">
<area shape="rect" coords="411,204,449,233" href="game.php?x=area20" target="_SELF">

</map>
</center><br /><br />';
echo '<center><a href="index.php">Back to Forum</a></center>';
}



Arantor

mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db('database_name') or die(mysql_error());


^^ can be removed. SSI.php does that itself anyway.


The problem is that in your function, $user_info is never pulled in from the global scope, so it's not available. Just add this just after the { in function main():
global $user_info;

brion

I tried global and it didn't check anything, still is blank. When I remove the database stuff at the top I get this error.

QuoteNo database selected
File: /home/mine/public_html/game.php
Line: 40

Arantor

Weird, you shouldn't have to do that. All that'll happen is you use a little extra memory.

$user_info is declared by SSI.php and what it loads. Since I can't see all your file I have to guess at parts of it but you will need to global it in any function you're using. I assumed it was main() that was doing it but on further examination that's not the case.

brion

This is the entire file. The rest didn't have anything to do with the functions.

<?php require("SSI.php");
mysql_connect('localhost''user''pass') or die(mysql_error());
mysql_select_db('database_name') or die(mysql_error());

if (isset(
$_GET["x"])) {
$x explode(":",$_GET["x"]);

    switch(
$x[0])
{
case 'area1':
area1();
break;

case 'area2':
area2();
break;

case 'area3':
area3();
break;

 }
}
 else { 
main(); }
?>


<html>
<head>
<title>Detectors Paradise - Metal Detecting Game</title>
</head>
<body bgcolor="tan">

<?php
function main() {
global 
$user_info;
echo 
'<table align="right"><tr><td>';
ssi_welcome();
echo 
'</td></tr></table>';
echo 
'<h2>Metal Detecting Game.</h2>';

$checkk db_query("SELECT clad FROM smf_members WHERE ID_MEMBER='{$user_info['id']}'"__FILE____LINE__);
$check2 mysql_fetch_array($checkk);

echo 
'<table width="20%" align="right">';
echo 
"<tr><td><b>Clad:</b> ".$check2['clad']."</td></tr>";
echo 
"<tr><td><b>Silver Coins:</b> 0</td></tr>";
echo 
'<tr><td><b>Relics:</b> 0</td></tr>';
echo 
'</table><br />';
echo 
'<center><img src="/game/park1.jpg" usemap="#park1" border="0">

<map name="park1">
<area shape="rect" coords="413,317,477,354" href="game.php?x=area1" target="_SELF">
<area shape="rect" coords="334,310,395,354" href="game.php?x=area2" target="_SELF">
<area shape="rect" coords="233,305,304,351" href="game.php?x=area3" target="_SELF">
<area shape="rect" coords="174,302,196,351" href="game.php?x=area4" target="_SELF">
<area shape="rect" coords="88,324,153,340" href="game.php?x=area5" target="_SELF">
<area shape="rect" coords="8,311,73,348" href="game.php?x=area6" target="_SELF">
<area shape="rect" coords="26,233,54,256" href="game.php?x=area7" target="_SELF">
<area shape="rect" coords="112,248,147,280" href="game.php?x=area8" target="_SELF">
<area shape="rect" coords="199,237,242,285" href="game.php?x=area9" target="_SELF">
<area shape="rect" coords="285,233,310,248" href="game.php?x=area10" target="_SELF">
<area shape="rect" coords="334,246,391,288" href="game.php?x=area11" target="_SELF">
<area shape="rect" coords="417,250,466,279" href="game.php?x=area12" target="_SELF">
<area shape="rect" coords="14,208,63,217" href="game.php?x=area13" target="_SELF">
<area shape="rect" coords="97,205,76,190" href="game.php?x=area14" target="_SELF">
<area shape="rect" coords="124,167,176,182" href="game.php?x=area15" target="_SELF">
<area shape="rect" coords="122,196,146,209" href="game.php?x=area16" target="_SELF">
<area shape="rect" coords="168,193,193,208" href="game.php?x=area17" target="_SELF">
<area shape="rect" coords="225,199,251,214" href="game.php?x=area18" target="_SELF">
<area shape="rect" coords="307,203,328,222" href="game.php?x=area19" target="_SELF">
<area shape="rect" coords="411,204,449,233" href="game.php?x=area20" target="_SELF">

</map>
</center><br /><br />'
;

echo 
'<center><a href="index.php">Back to Forum</a></center>';
}

function 
area1() {
echo 
'<center><b>You found a chewing gum wrapper!</b><br /><br /><img src="/game/gum.png"></center>';
echo 
'<center><img src="/game/park1.jpg" usemap="#park1" border="0">

<map name="park1">
<area shape="rect" coords="413,317,477,354" href="game.php?x=area1" target="_SELF">
<area shape="rect" coords="334,310,395,354" href="game.php?x=area2" target="_SELF">
<area shape="rect" coords="233,305,304,351" href="game.php?x=area3" target="_SELF">
<area shape="rect" coords="174,302,196,351" href="game.php?x=area4" target="_SELF">
<area shape="rect" coords="88,324,153,340" href="game.php?x=area5" target="_SELF">
<area shape="rect" coords="8,311,73,348" href="game.php?x=area6" target="_SELF">
<area shape="rect" coords="26,233,54,256" href="game.php?x=area7" target="_SELF">
<area shape="rect" coords="112,248,147,280" href="game.php?x=area8" target="_SELF">
<area shape="rect" coords="199,237,242,285" href="game.php?x=area9" target="_SELF">
<area shape="rect" coords="285,233,310,248" href="game.php?x=area10" target="_SELF">
<area shape="rect" coords="334,246,391,288" href="game.php?x=area11" target="_SELF">
<area shape="rect" coords="417,250,466,279" href="game.php?x=area12" target="_SELF">
<area shape="rect" coords="14,208,63,217" href="game.php?x=area13" target="_SELF">
<area shape="rect" coords="97,205,76,190" href="game.php?x=area14" target="_SELF">
<area shape="rect" coords="124,167,176,182" href="game.php?x=area15" target="_SELF">
<area shape="rect" coords="122,196,146,209" href="game.php?x=area16" target="_SELF">
<area shape="rect" coords="168,193,193,208" href="game.php?x=area17" target="_SELF">
<area shape="rect" coords="225,199,251,214" href="game.php?x=area18" target="_SELF">
<area shape="rect" coords="307,203,328,222" href="game.php?x=area19" target="_SELF">
<area shape="rect" coords="411,204,449,233" href="game.php?x=area20" target="_SELF">

</map>
</center><br /><br />'
;
echo 
'<center><a href="index.php">Back to Forum</a></center>';
}

function 
area2() {
echo 
'<center><b>You found a memorial penny!</b><br /><br /><img src="/game/cladpenny.jpg"></center>';
db_query("UPDATE smf_members SET clad=clad+1 WHERE ID_MEMBER='{$user_info['id']}'"__FILE____LINE__) or die( mysql_error());
echo 
'<center><img src="/game/park1.jpg" usemap="#park1" border="0">

<map name="park1">
<area shape="rect" coords="413,317,477,354" href="game.php?x=area1" target="_SELF">
<area shape="rect" coords="334,310,395,354" href="game.php?x=area2" target="_SELF">
<area shape="rect" coords="233,305,304,351" href="game.php?x=area3" target="_SELF">
<area shape="rect" coords="174,302,196,351" href="game.php?x=area4" target="_SELF">
<area shape="rect" coords="88,324,153,340" href="game.php?x=area5" target="_SELF">
<area shape="rect" coords="8,311,73,348" href="game.php?x=area6" target="_SELF">
<area shape="rect" coords="26,233,54,256" href="game.php?x=area7" target="_SELF">
<area shape="rect" coords="112,248,147,280" href="game.php?x=area8" target="_SELF">
<area shape="rect" coords="199,237,242,285" href="game.php?x=area9" target="_SELF">
<area shape="rect" coords="285,233,310,248" href="game.php?x=area10" target="_SELF">
<area shape="rect" coords="334,246,391,288" href="game.php?x=area11" target="_SELF">
<area shape="rect" coords="417,250,466,279" href="game.php?x=area12" target="_SELF">
<area shape="rect" coords="14,208,63,217" href="game.php?x=area13" target="_SELF">
<area shape="rect" coords="97,205,76,190" href="game.php?x=area14" target="_SELF">
<area shape="rect" coords="124,167,176,182" href="game.php?x=area15" target="_SELF">
<area shape="rect" coords="122,196,146,209" href="game.php?x=area16" target="_SELF">
<area shape="rect" coords="168,193,193,208" href="game.php?x=area17" target="_SELF">
<area shape="rect" coords="225,199,251,214" href="game.php?x=area18" target="_SELF">
<area shape="rect" coords="307,203,328,222" href="game.php?x=area19" target="_SELF">
<area shape="rect" coords="411,204,449,233" href="game.php?x=area20" target="_SELF">

</map>
</center><br /><br />'
;
echo 
'<center><a href="index.php">Back to Forum</a></center>';
}

function 
area3() {
echo 
'<center><b>You found a modern nail!</b><br /><br /><img src="/game/modnail.jpg"></center>';
echo 
'<center><img src="/game/park1.jpg" usemap="#park1" border="0">

<map name="park1">
<area shape="rect" coords="413,317,477,354" href="game.php?x=area1" target="_SELF">
<area shape="rect" coords="334,310,395,354" href="game.php?x=area2" target="_SELF">
<area shape="rect" coords="233,305,304,351" href="game.php?x=area3" target="_SELF">
<area shape="rect" coords="174,302,196,351" href="game.php?x=area4" target="_SELF">
<area shape="rect" coords="88,324,153,340" href="game.php?x=area5" target="_SELF">
<area shape="rect" coords="8,311,73,348" href="game.php?x=area6" target="_SELF">
<area shape="rect" coords="26,233,54,256" href="game.php?x=area7" target="_SELF">
<area shape="rect" coords="112,248,147,280" href="game.php?x=area8" target="_SELF">
<area shape="rect" coords="199,237,242,285" href="game.php?x=area9" target="_SELF">
<area shape="rect" coords="285,233,310,248" href="game.php?x=area10" target="_SELF">
<area shape="rect" coords="334,246,391,288" href="game.php?x=area11" target="_SELF">
<area shape="rect" coords="417,250,466,279" href="game.php?x=area12" target="_SELF">
<area shape="rect" coords="14,208,63,217" href="game.php?x=area13" target="_SELF">
<area shape="rect" coords="97,205,76,190" href="game.php?x=area14" target="_SELF">
<area shape="rect" coords="124,167,176,182" href="game.php?x=area15" target="_SELF">
<area shape="rect" coords="122,196,146,209" href="game.php?x=area16" target="_SELF">
<area shape="rect" coords="168,193,193,208" href="game.php?x=area17" target="_SELF">
<area shape="rect" coords="225,199,251,214" href="game.php?x=area18" target="_SELF">
<area shape="rect" coords="307,203,328,222" href="game.php?x=area19" target="_SELF">
<area shape="rect" coords="411,204,449,233" href="game.php?x=area20" target="_SELF">

</map>
</center><br /><br />'
;
echo 
'<center><a href="index.php">Back to Forum</a></center>';
}

?>

</body>
</html>

Arantor

Except that you have several functions, area1(), area2() which call DB queries, which as I said you'll need to global first.

brion

Right, that is true, but right now I just want to get the main function to work correctly by showing the number that is in the database per user. All of them have a default of 0, so it should at least show 0 in the clad section, not blank.

Arantor

Did you actually set it to 0 when constructing the new column... presumably in phpMyAdmin?

brion

Yes in PHPmyAdmin, when I make the 7 fields, I put 0 in the default.

Arantor


brion

Yeah all of them, except on my account my clad is 3, because when I did

WHERE ID_MEMBER='1'

as a test, it updated my account, but regardless it should still show 3 on my main function page.

Arantor

And you are logged in at the time this happens and you get an appropriate welcome message?

Hmm.

brion

Yes it says Hey, fayt, you have 133 messages, 0 are new.

Compared to the please register or login message.

It's so weird that this is happening.

Arantor

In that case, global $context and use $context['user']['id'] instead of $user_info['id'].

brion

Perfect, it works now. Thanks so much. I have one more question. Do you know a quick and easy code that I can put in each function, that if they aren't logged into the forum, then it shows an error message?

Something like this

if (!empty($session)) {
//Show the function
}
else {
echo 'Please login to the forum to play this game';
}

Arantor

Put this just after SSI.php. It'll give them a message and stop the rest of the page loading.

if($context['user']['is_guest'])
  die('Please log in to the forum to play this game');

brion


Advertisement: