Jump to content

Tie together two seperate bits of PHP?


ffxpwns

Recommended Posts

Okay, I have posted before, but I truly cannot get this working.  I wans a script where upon entering a page, if someone referred that person, they are awarded 5$.  I thought I had it down, but it will not set the column 'refer' to null after the page laods, as it should.  And it will not award the referrer with the money if the referrer has any information in their 'refer' column.  Please let me know if that made sense or not, and I will try and clarify.  Here is the code I'm trying to add:

 

 $query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referName = $gateway_data['refer'];

$query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referred)."'";
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referred = $gateway_data['nick'];

 

And heres the code I'm trying to add that ^ to. http://pastebin.com/rwcN7FeJ I tried adding it at around line 12.  Theres one more thing, if I do this (code below) it does it correctly, but it clears all the 'refer' data for everyone, not just the one user.

 

 $query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referName = $gateway_data['refer'];

$query = "UPDATE ".$DBPrefix."users SET refer = ' ' ";
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);

 

 

Essentially, I want to do this:

'nick'    'refer'    'balance'

--------+---------+---------+

| a      |    b      | 0.00    |

--------+----------+--------+

|b      |  c          | 0 .00  |

--------+----------+--------|

|c      |  a        | 0.00  |

------------------------------

 

Then user 'a' buys  something, thus 'b' gets the bonus for referring them,  and 'a' gets their refer set to a null value like:

 

'nick'    'refer'    'balance'

--------+---------+---------+

| a      | NULL    | 0.00    |

--------+----------+--------+

|b      |  c          | 5 .00  |

--------+----------+--------|

|c      |  a          | 0.00  |

------------------------------

 

Link to comment
Share on other sites

In your first bit of code:

$query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referName = $gateway_data['refer'];

$query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referred)."'";
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referred = $gateway_data['nick'];

 

You seem to be setting the $query with variables that you create afterwards.

 

So, in the first query:

$query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'";

$referName doesn't exist, so if you were to

echo $query;

, it would show:

UPDATE mydb.users SET balance = balance + 5 WHERE refer = ''

 

What you need to do is to get the referrer from the database first, I'd do this by adding a function:

function getReferName($n)
{
  $query = "SELECT refer FROM ".$DBPrefix."users WHERE nick = '".mysql_real_escape_string($n)."'";
  $res = mysql_query($query);
  $system->check_mysql($res, $query, __LINE__, __FILE__);

  /* Should only be one result */
  $row = mysql_fetch_assoc($res);
  $referName = $row['refer'];

  return ($referName);
}

 

Now, you can use that in your script like:

// Assume we have $userName

$referName = getReferName($userName);  // Who referred userName?

$query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($userName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);

$query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);

 

Note that I removed the $gateway_data lines from each query - they seemed to be doing nothing useful.  Put them back in only if you needed the variables after this point...

Link to comment
Share on other sites

In your first bit of code:

$query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referName = $gateway_data['refer'];

$query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referred)."'";
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referred = $gateway_data['nick'];

....

Well, major thing, it doesn't work.  It says

Fatal error: Call to a member function check_mysql() on a non-object in /.../html/pennybid/pay.php on line 18

And line 18 is shown in the group of text here (just to give context)

 

 $query = "SELECT refer FROM ".$DBPrefix."users WHERE nick = '".mysql_real_escape_string($n)."'";
  $res = mysql_query($query);
/*line 18 below*/
  $system->check_mysql($res, $query, __LINE__, __FILE__);
/*line 18 above*/
  /* Should only be one result */ 
  $row = mysql_fetch_assoc($res);
  $referName = $row['refer'];

 

But anyways, I am calling $userName like so:

$userName =  $user->user_data['nick'];

 

And I'll put it in context just to make sure I'm doing it right.

function getReferName($n)
{
  $query = "SELECT refer FROM ".$DBPrefix."users WHERE nick = '".mysql_real_escape_string($n)."'";
  $res = mysql_query($query);
  $system->check_mysql($res, $query, __LINE__, __FILE__);

  /* Should only be one result */ 
  $row = mysql_fetch_assoc($res);
  $referName = $row['refer'];


  return ($referName);
}

$userName =  $user->user_data['nick'];

$referName = getReferName($userName);  // Who referred userName?

$query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($userName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);

$query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);

Link to comment
Share on other sites

And the call for userName does work, say I'm logged in as ffxpwns, I put in $userName =  $user->user_data['nick']; then print_r ($userName); it says ffxpwns.  So I assume that's not broken.  This may seem super obvious to you, but PHP isn't my strongpoint.

Link to comment
Share on other sites

That is an awesome picture.

 

$system is not defined within the context of that function. You said when you comment out line 18 you still get the error? I can only assume it changes to a different line number? Because you have $system->check_mysql in 3 places there, but $system is never defined.

Link to comment
Share on other sites

That is an awesome picture.

 

$system is not defined within the context of that function. You said when you comment out line 18 you still get the error? I can only assume it changes to a different line number? Because you have $system->check_mysql in 3 places there, but $system is never defined.

 

Thanks! Everyone love puppies.

 

Now when I comment it out, no error is incited. But nothing happens to the balance / refer fields.

Link to comment
Share on other sites

Also, when I use my old code (below) and I put the $refername or $referrer or whatever it is above the code, and I echo $query, it echos UPDATE DataBase_users SET balance = balance + 5 WHERE refer = ''.  Almost as if it doesn't recognize it or something.  And I'm sure my spelling is correct :P.  I'm pretty sure PHP is out to get me. 

 

CODE:

$query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'";

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referName = $gateway_data['refer'];

$query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referred)."'";
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$gateway_data = mysql_fetch_assoc($res);
$referred = $gateway_data['nick'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.