kaosjon Posted September 18, 2011 Share Posted September 18, 2011 Hi, i am just adding the last component to my registration page, but i am having a little trouble with it. Basically my website includes a referal based system, wherebye when the user signs up they can add a referal that benefits the referal later on. The code i am trying to add is to increase the number of referals a member has by 1. It might sound a bit confusing and trust me its taken a few hours to get my head around it, but my code must find the information of the referal and update their number of referals by 1. The code i have below is what i think should work, but it does not update the number of referals by 1. if($referal_check > 0) { //Select everything from user_info where the username is the same as the referal entered by registering user $sql_referal = ("SELECT * FROM user_info WHERE username = $referal"); $result = mysql_query($sql_result); $referal_fetch = mysql_fetch_array($result); //Select everything from referrals table where the id in the table is the same as the id from user from $sql_referal query $sql_referrals = ("SELECT * FROM referrals WHERE id = $id"); $result_referal = mysql_query($sql_referrals); $referrals_fetch = mysql_fetch_array($result_referal); //Update referrals table setting the no_of referals to + 1 where the id is the same as the id from $sql_referrals mysql_query("UPDATE referrals SET no_of_referrals = no_of_referrals + '1' WHERE id = $id"); } The referal_check is a query to find whether the referal exists in the system, the code is below $sql_referal_check = mysql_query("SELECT username FROM user_info WHERE username='$referal'"); $referal_check = mysql_num_rows($sql_referal_check); Sorry if this is a bit confusing, please ask if you need some more clarification on the code. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247380-not-updating-number-of-referals/ Share on other sites More sharing options...
PaulRyan Posted September 18, 2011 Share Posted September 18, 2011 You've used the variable $id twice and I cannot see where you set this value. Echo out the variable $id and see what it holds, I'm guessing it holds nothing and you will need to set it with the ID of the referring user. Try the above out and tell me how it goes. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/247380-not-updating-number-of-referals/#findComment-1270412 Share on other sites More sharing options...
kaosjon Posted September 18, 2011 Author Share Posted September 18, 2011 Hi, i thought about what you said and i think i found an easier way to do this by adding the username field to the referrals table, that way i don't have to keep searching for the id, i can use the referal variable already stored in the code higher up in the script. Heres the new code but it still does not work if($referal_check > 0) { mysql_query("UPDATE referrals SET no_of_referrals = no_of_referrals + '1' WHERE username = '$referal'"); } I tested the query in the sql query page in phpmyadmin changing the $referal to the referal a user would enter and it worked, but for some reason it is not working when i add the query to my site. Any other ideas, thanks Quote Link to comment https://forums.phpfreaks.com/topic/247380-not-updating-number-of-referals/#findComment-1270414 Share on other sites More sharing options...
PaulRyan Posted September 21, 2011 Share Posted September 21, 2011 It really does sound like a variable is not being set, try echoing out $referal_check AND $referal to see what values they hold, if they are holding the correct values then it may be something else that I have missed, I'll be able to havea proper look at your code abit later today. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/247380-not-updating-number-of-referals/#findComment-1271283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.