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

 

?>

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

?>

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

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.