View Full Version : Add 50 each time someone enters a Username

03-27-2007, 07:52 PM
Hey guys,

I've been trying to add a referrer mod to my PHPBB, but have had little luck so I am attempting to make my own.

I want to make a PHP script that will add 50 to a table field in MySQL every time someone enters a username into a field.

Here is the HTML I am going to add

<input type="text" value="Referrer" />

I want that to add 50 to my MySQL table field "user_refer."

If the user does not exist I want it to ignore that part of the PHP and move on.

So, lets say Johnny joins the forum and was referred by Josh. Josh has 500 referral points. If Johnny submits the form with Josh's name in the Refer field then Josh should receive 50 more points thus making 550. If he spells it Josh "Jsoh," I want the script to continue so Johnny still becomes a registered member.

I am still learning about PHP but I think I got an idea with what I need to do...

Something like

$refer = $_POST['refer']

SELECT user_refer FROM phpbb_users WHERE username = "$refer";

Now, that I have it I need to add 50 to it and then reinsert it.

But, I do not know how to make the above line a variable or how to copy over the existing MySQL entry of 500 and make it 550.

ralph l mayo
03-27-2007, 08:01 PM
UPDATE phpbb_users SET user_refer = user_refer + 50 WHERE username="$refer"

mind that $refer is escaped or validated.

03-27-2007, 08:36 PM
why not have referal links for each referer
that way it will save your users adding the username to who refered them
then just use an if (http://php.net/if) statement to see if in isset() (http://php.net/function.isset)
if it is run the statement ralph suggested above

03-27-2007, 08:41 PM
Ralph, wouldn't a receive errors if $refer doesn't exist in the database? How would I get around that?

"mind that $refer is escaped or validated."

That may be answering my question above, but I am not positive what it means.

I think you are saying that $refer will either work or not, thus allowing the rest of the script to parse without receiving errors. I may be wrong.

Rafiki, the reason I am doing it this way is because some people may be referred from word of mouth, not just links.

Although, how would I do what you are saying?

Like this?

link = mysite.com/register.php?refer=username

if (isset($refer)) {
UPDATE phpbb_users SET user_refer = user_refer + 50 WHERE username="$refer"

03-27-2007, 08:46 PM
Because not everyone will be referred through a link. Some people may be referred through word of mouth.
ok but you could still run the if statement and if in isnt set, ask them where they found yoursite, add some javascript to add anouther field for the appropriate

03-27-2007, 09:00 PM
Well, that is what I think I am going to do now that you mention it.

If the user did not connect through a referral link, then the <input> tag for Referrer will show up, otherwise it will just be a hidden input with a value of $refer.

Something like this I think

if (isset($refer)) {
echo('<input type=\"hidden\" value=\"$refer\" />')

else {
echo('<input type=\"text\" value=\"Referrer\" />')

03-27-2007, 09:02 PM
you got it :)

03-27-2007, 10:42 PM
Ralph, wouldn't a receive errors if $refer doesn't exist in the database? How would I get around that?

An update query that doesn't update anything will not throw an error. You can call the function mysql_affected_rows() (http://us2.php.net/manual/en/function.mysql-affected-rows.php) to find out how many rows the update query updated, and take action from there.