Jump to content

Use data from one row for another?


ffxpwns

Recommended Posts

Okay, my table structure has columns called 'nick' (username) 'refer' (Who referred this person) and 'balance'  There are a lot of other ones, but these are the ones that matter.  So when someone signs up, they put in their referrers username and it does nothing.  And when the referred user buys something, the referrer gets 5$.  Here is my question.  How would I write an UPDATE where the name in the refer field would award the referrer with +5 to balance, then it would clear the 'refer' field.  I got the set refer to blank.  But I'm not very good with MySQL commands.  Heres what I have

 

$query = "UPDATE " . $DBPrefix . "users SET refer=' ' 

Link to comment
Share on other sites

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

| nick | refer | balance    |

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

|  A  |  B    |  0.00    |

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

|  B  |  C    |  0.00    |

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

 

A buys so B gets $5 added to his balance and the referrer is cleared from A, giving this after the update

 

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

| nick | refer | balance    |

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

|  A  |      |  0.00    |

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

|  B  |  C    |  5.00    |

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

 

Is this scenario correct?

Link to comment
Share on other sites

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

| nick | refer | balance    |

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

|  A  |  B    |  0.00    |

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

|  B  |  C    |  0.00    |

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

 

A buys so B gets $5 added to his balance and the referrer is cleared from A, giving this after the update

 

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

| nick | refer | balance    |

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

|  A  |      |  0.00    |

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

|  B  |  C    |  5.00    |

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

 

Is this scenario correct?

 

That looks prefect to me!

Link to comment
Share on other sites

in which case

 

$user = 'A';


UPDATE users a
INNER JOIN users b ON a.refer = b.nick
SET a.refer = '', b.balance = b.balance + 5
WHERE a.nick = '$user'

 

Sorry, I'm really new to PHP, How would I call the users A and B?  I have my code here http://pastebin.com/rwcN7FeJ.  You can see on line 241 is where I was trying to do this.  This is an open source auction script called WeBid.

Link to comment
Share on other sites

in my example, user A is the one who bought the goods. B is A's referrer and gets the $5 commission

 

Yes, but you have

$user = 'A';


UPDATE users a
INNER JOIN users b ON a.refer = b.nick
SET a.refer = '', b.balance = b.balance + 5
WHERE a.nick = '$user'

 

And I do not understand how I would define user A and B.  Could you please look at a little snippet of the code and tell me how to do this?  I'm so lost.

Link to comment
Share on other sites

I changed my query slightly as you are coming at it using "refer" rather than "nick".

 

$referName = mysql_real_escape_string($gateway_data['refer']);
$query = "UPDATE users a
        INNER JOIN users b ON a.refer = b.nick
        SET a.refer = '', b.balance = b.balance + 5
        WHERE b.refer = '$referName'";
mysql_query($query);

Link to comment
Share on other sites

I changed my query slightly as you are coming at it using "refer" rather than "nick".

 

$referName = mysql_real_escape_string($gateway_data['refer']);
$query = "UPDATE users a
        INNER JOIN users b ON a.refer = b.nick
        SET a.refer = '', b.balance = b.balance + 5
        WHERE b.refer = '$referName'";
mysql_query($query);

 

I'm sorry, sir, but I still don't understand how I would select who user A and B were?  http://pastebin.com/rwcN7FeJ at line 22, I have add to account.  Maybe it could be incorporated there?

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.