FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Make MySQL value Increase by one, using PHP





ftv_flung
I'm trying to set something up whereby a number in a field goes up 1 each time another member is referred using their link. I've got everything else set up fine, but I'd like to know how to make the "number of referrals" go up by 1 each time a registration takes place...

Thanks - I hope this makes some sense!
b4r4t
Well ... You can increment directly in MySQL.

For example You have table referrals, and there You have the referrals_number field ... then just:

UPDATE referrals SET referrals_number = 'referrals_number + 1';

Then Everytime You want to increment just use this query Smile
ftv_flung
Thanks b4r4t, but hmm... still not working properly...

Here's the code

"user_referred_by" is the username that has referred the new user
"referrerscore" is the referrers score (which I'm trying to increase each time a new user joins because of the referrer)

Code:

$selectfromusers = mysql_query("SELECT * FROM users")
or die(mysql_error());

$rfrscr = $row[referrerscore];

$addition = mysql_query("UPDATE users SET referrerscore='$rfrscr + 1' WHERE username='$user_referred_by'")
or die(mysql_error());
b4r4t
ftv_flung wrote:
Thanks b4r4t, but hmm... still not working properly...

Here's the code

"user_referred_by" is the username that has referred the new user
"referrerscore" is the referrers score (which I'm trying to increase each time a new user joins because of the referrer)

Code:

$selectfromusers = mysql_query("SELECT * FROM users")
or die(mysql_error());

$rfrscr = $row[referrerscore];

$addition = mysql_query("UPDATE users SET referrerscore='$rfrscr + 1' WHERE username='$user_referred_by'")
or die(mysql_error());


You don't have to get $row[referrerscore] ... just try:


Code:


$selectfromusers = mysql_query("SELECT * FROM users")
or die(mysql_error());

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`='$user_referred_by'")
or die(mysql_error());

ftv_flung
grrr... it's still not updating "referrerscore" for some reason Sad

Edit: Okay it's worked!!!! I didn't copy and paste it correctly ? haha thanks sooo much!!
b4r4t
No problem Smile Good luck with the rest of your script Smile

By the way - if You don't use $selectfromusers in this script then You may delete this query ...
ftv_flung
oh, I use just using that to remind me what it was for haha

I guess I should use the "//"s
kv
Quote:

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`='$user_referred_by'") ;


This does not work since '$user_referred_by' is within single quotes and will not be parsed by php. Use this instead.

Code:

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`=' ".$user_referred_by." ' ") ;
rehana
kv wrote:
Quote:

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`='$user_referred_by'") ;


This does not work since '$user_referred_by' is within single quotes and will not be parsed by php. Use this instead.

Code:

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`=' ".$user_referred_by." ' ") ;



Brother Nice Effort To Do

i think MySQL Is The Hard To Learn
b4r4t
rehana wrote:
kv wrote:
Quote:

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`='$user_referred_by'") ;


This does not work since '$user_referred_by' is within single quotes and will not be parsed by php. Use this instead.

Code:

$addition = mysql_query("UPDATE `users` SET `referrerscore`= referrerscore + 1 WHERE `username`=' ".$user_referred_by." ' ") ;



Brother Nice Effort To Do

i think MySQL Is The Hard To Learn



This is not MySQL issue but PHP. Single and Double Quotes are different - just check it in PHP manual Smile

Check this:

Code:

<?php

$var = '<strong>variable</strong>';


echo "<p>this is $var in double quotes</p>";

echo '<p>this is $var in single quotes</p>';

echo "<p>this is ".$var." which was escaped in double quotes but this isn't necessary</p>";

echo '<p>this is '.$var.' which was escaped in single quotes</p>';

echo '<p>Thanks to single quotes I can use "double quotes" with no escape sign but I have to escape every '.$var.'</p>';

echo "<p>Thanks to double quotes I can use $var with no escape, but I have to escape every \"double quotes\"</p>";

?>


You have to decide which method is best in the moment Smile

For example double quotes are great for normal sentences with few variables in it like

Code:

<?php

$var1 = 'variable 1';
$var2 = 'variable 2';
$var3 = 'variable 3';
$var4 = 'variable 4';

echo "this script contain four variables: $var1, $var2, $var3 and $var4";

?>


Single can be cool for html optput where you use lot of double quotes like

Code:

<?php

$var = '<a href="#" title="title text"><img src="img.jpg" alt="alt text" /></a>';

echo $var;

?>
rvec
that's not really the best way to do it. You should read this to see which quotes you should use and when to use them:
http://www.phpbench.com/
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Need Help in PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
"Drop Down List Box" Using PHP and MySql
MySQL Database Backup Using PHP
Help displaying pdfs using PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.