Jump to content

[SOLVED] PLEASE HELP ASAP (If else within If else)


tommyda

Recommended Posts

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';}

 

?>

Link to comment
Share on other sites

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');}
}

?>

Link to comment
Share on other sites

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');}
}
}

?>

Link to comment
Share on other sites

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';
}
}
?>

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.