tommyda Posted October 7, 2007 Share Posted October 7, 2007 I am trying to build a rating system for my website but i am stuck as i need to check whether a user is logged in and if they ARE then check wether they have already rated and if they HAVE then rate but if they HAVENT say "sorry you have already rated" But if they are not logged direct them to the login page and say "you must be logged in to rate a site <?php include"include/session.php"; include"includes/mysql.php"; //check if logged in if($session->logged_in){ $user = $session->username; $rating=$_GET['rating']; $site=$_GET['affid']; //if logged in check whether this user has rated this site $con = "SELECT * FROM ratings WHERE user='$user' AND affid='$affid';"; $res = mysql_query($con); if (mysql_num_rows($res) > 0) {echo'You have already rated this page';} // yes, pull in the user details } else{ // no, user doesn't exist $insert ="INSERT INTO ratings (user, rating, affid) VALUES ('$user','$rating','$affid')"; $insert2 = mysql_query($insert); if(!$insert2) die(mysql_error()); echo('Thank you for rating');} } else{die'please login';} ?> Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/ Share on other sites More sharing options...
Wuhtzu Posted October 7, 2007 Share Posted October 7, 2007 What seem to be the problem? We can't (and most probably wont even if they could) run your code so please specify what is not working. Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363975 Share on other sites More sharing options...
d.shankar Posted October 7, 2007 Share Posted October 7, 2007 First try to make proper indentations before posting the code buddy.. I have removed some unneccessary else statements and braces. Check the code now <?php include "include/session.php"; include "includes/mysql.php"; //check if logged in if($session->logged_in) { $user = $session->username; $rating=$_GET['rating']; $site=$_GET['affid']; //if logged in check whether this user has rated this site $con = "SELECT * FROM ratings WHERE user='$user' AND affid='$affid';"; $res = mysql_query($con); if (mysql_num_rows($res) > 0) { echo'You have already rated this page'; } // yes, pull in the user details } else { // no, user doesn't exist $insert ="INSERT INTO ratings (user, rating, affid) VALUES ('$user','$rating','$affid')"; $insert2 = mysql_query($insert); if(!$insert2) { die(mysql_error()); } echo('Thank you for rating');} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363980 Share on other sites More sharing options...
tommyda Posted October 7, 2007 Author Share Posted October 7, 2007 There is no error at the moment but i need to put a Else{location: login.php} at the end of the page and its not working is there any other way of putting if if else else in that order Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363986 Share on other sites More sharing options...
d.shankar Posted October 7, 2007 Share Posted October 7, 2007 Then try this <?php include "include/session.php"; include "includes/mysql.php"; //check if logged in if(empty($session->logged_in) || !isset($session->logged_in)) { header("location:login.php"); } else { if($session->logged_in) { $user = $session->username; $rating=$_GET['rating']; $site=$_GET['affid']; //if logged in check whether this user has rated this site $con = "SELECT * FROM ratings WHERE user='$user' AND affid='$affid';"; $res = mysql_query($con); if (mysql_num_rows($res) > 0) { echo'You have already rated this page'; } // yes, pull in the user details } else { // no, user doesn't exist $insert ="INSERT INTO ratings (user, rating, affid) VALUES ('$user','$rating','$affid')"; $insert2 = mysql_query($insert); if(!$insert2) { die(mysql_error()); } echo('Thank you for rating');} } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363989 Share on other sites More sharing options...
tommyda Posted October 7, 2007 Author Share Posted October 7, 2007 It works perfectly and i have learned a new way round handling ifelse's thank you! Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363995 Share on other sites More sharing options...
haaglin Posted October 7, 2007 Share Posted October 7, 2007 If $session->logged_in is a boolean, then try this: <?php include "include/session.php"; include "includes/mysql.php"; //check if logged in if(!$session->logged_in) { header("location:login.php"); } else { $user = $session->username; $rating=$_GET['rating']; $site=$_GET['affid']; //if logged in check whether this user has rated this site $con = "SELECT * FROM ratings WHERE user='$user' AND affid='$affid';"; $res = mysql_query($con); if (mysql_num_rows($res) > 0) { echo 'You have already rated this page'; } // yes, pull in the user details else { // no, user doesn't exist $insert ="INSERT INTO ratings (user, rating, affid) VALUES ('$user','$rating','$affid')"; $insert2 = mysql_query($insert); if(!$insert2) { die(mysql_error()); } echo 'Thank you for rating'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363997 Share on other sites More sharing options...
d.shankar Posted October 7, 2007 Share Posted October 7, 2007 It works perfectly and i have learned a new way round handling ifelse's thank you! No probs.. I am glad that the code found useful for ya. Do click Topic Solved if your problem is clarified. Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-363998 Share on other sites More sharing options...
tommyda Posted October 7, 2007 Author Share Posted October 7, 2007 http://www.pokerdepositoptions.com/rate.php?affid=2&rating=5 <<<<RATE http://www.pokerdepositoptions.com/displayrating.php?affid=2 <<<<DISPLAY RATING Works like a dream Quote Link to comment https://forums.phpfreaks.com/topic/72180-solved-please-help-asap-if-else-within-if-else/#findComment-364000 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.