Advertisement:

Author Topic: Linking queries to smf database per user  (Read 3636 times)

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #20 on: November 10, 2009, 04:43:31 PM »
Here is the top of the file

Code: [Select]
<?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.

Code: [Select]
<?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.

Code: [Select]
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>';
}


Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #21 on: November 10, 2009, 04:45:11 PM »
Code: [Select]
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():
Code: [Select]
global $user_info;
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #22 on: November 10, 2009, 05:16:03 PM »
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.

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

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #23 on: November 10, 2009, 05:19:21 PM »
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.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #24 on: November 10, 2009, 05:43:59 PM »
This is the entire file. The rest didn't have anything to do with the functions.

Code: [Select]
<?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>

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #25 on: November 10, 2009, 05:47:23 PM »
Except that you have several functions, area1(), area2() which call DB queries, which as I said you'll need to global first.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #26 on: November 10, 2009, 06:40:57 PM »
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.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #27 on: November 10, 2009, 06:42:40 PM »
Did you actually set it to 0 when constructing the new column... presumably in phpMyAdmin?
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #28 on: November 10, 2009, 06:59:14 PM »
Yes in PHPmyAdmin, when I make the 7 fields, I put 0 in the default.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #29 on: November 10, 2009, 07:00:12 PM »
And are the fields actually 0 right now?
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #30 on: November 10, 2009, 08:34:27 PM »
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.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #31 on: November 10, 2009, 08:39:01 PM »
And you are logged in at the time this happens and you get an appropriate welcome message?

Hmm.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #32 on: November 10, 2009, 09:56:30 PM »
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.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #33 on: November 10, 2009, 10:06:53 PM »
In that case, global $context and use $context['user']['id'] instead of $user_info['id'].
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #34 on: November 11, 2009, 09:39:53 AM »
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

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

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,782
    • StoryBB/StoryBB on GitHub
Re: Linking queries to smf database per user
« Reply #35 on: November 11, 2009, 09:55:20 AM »
Put this just after SSI.php. It'll give them a message and stop the rest of the page loading.

Code: [Select]
if($context['user']['is_guest'])
  die('Please log in to the forum to play this game');
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline brion

  • Jr. Member
  • **
  • Posts: 325
Re: Linking queries to smf database per user
« Reply #36 on: November 11, 2009, 01:23:30 PM »
Works perfect, thanks again