Jump to content

Multiple IF statement not working


dapcigar
 Share

Go to solution Solved by mac_gyver,

Recommended Posts

Hello all,

 

Am trying to use multiple if statement to check for data in the DB. everything seems fine till the last statement which is to insert the data in the DB if all the conditions are false. please, what am i doing wrong?

$tool = mysql_query("SELECT *  FROM leave_member WHERE firstname = '$firstname' AND lastname = '$lastname' ");
		$fest = mysql_fetch_array($tool);
		
		if ($type == 'Annual Leave'){
			
			
			$annual = $fest['annual'];
			if ($annual == '0')
			{
				echo "Sorry, you don't have any $type leave days available.";
				exit;
			}
			else if ($workingDays > $annual){
			
			echo "Sorry, the number of days you are requesting is more than the number of days you have left. ";
			
			
			exit;
		}
			
		 exit;	
		}
				
	else if ($type == 'Sick Leave'){
		
		$sick = $fest['sick'];
			if ($sick == '0')
			{
				echo "Sorry, you don't have any $type leave days available.";
				exit;
			}
			else if ($workingDays > $sick){
			
			echo "Sorry, the number of days you are requesting is more than the number of days you have left. ";
			
			
			exit;
		}
			
			exit;
		
	}
	
		else if ($type == 'Compassionate Leave'){
		
		$com = $fest['compassionate'];
				if ($com == '0')
			{
				echo "Sorry, you don't have any leave days available.";
				exit;
			}
			else if ($workingDays > $com){
			
			echo "Sorry, the number of days you are requesting is more than the number of days you have left. ";
			
			
			exit;
		}
			exit;
		
	}
	
			else if ($type == 'Study Leave'){
		
		$study = $fest['study'];
			//$result = $study - $days;
			//$com = $fest['study'];
				if ($study == '0')
			{
				echo "Sorry, you don't have any leave days available.";
				exit;
			}
			else if ($workingDays > $study){
			
			echo "Sorry, the number of days you are requesting is more than the number of days you have left. ";
			
			
			exit;
		}
			exit;
		
	}
	
				else if ($type == 'Mertanity Leave'){
		
		$maternity = $fest['maternity'];
			
				if ($maternity == '0')
			{
				echo "Sorry, you don't have any leave days available.";
				exit;
			}
			else if ($workingDays > $maternity){
			
			echo "Sorry, the number of days you are requesting is more than the number of days you have left. ";
			
			
			exit;
		}
			exit;
		
	}
	
	
		
	$sql = mysql_query("INSERT INTO leave_request ( id, firstname, lastname, department,  type, days, startdate, enddate, details, status) VALUES ( NULL, '$firstname', '$lastname', '$department', '$type','$workingDays', '$startDate', '$enddate','$details', '$status' )") or die(mysql_error());

 

 

 

 

 

 

 

thanks in advance

Link to comment
Share on other sites

You have   exit;  at the end of each of your if/elseif statements (see below what i mean). This will halt your script no matter if your conditions are met or not.

if ($type == 'Annual Leave'){
			
			
			$annual = $fest['annual'];
			if ($annual == '0')
			{
				echo "Sorry, you don't have any $type leave days available.";
				exit;
			}
			else if ($workingDays > $annual){
			
			echo "Sorry, the number of days you are requesting is more than the number of days you have left. ";
			
			
			exit;
		}
			
		 exit;	 // <---- this will kill the script no matter what
		}

Instead of using exit all over the place.

 

You should instead use a variable,  which you initially set to true. You will set this to false when your data does not validate. You'd then only execute the query if this variable is still true.

  • Like 1
Link to comment
Share on other sites

  • Solution

here's a different approach to your coding. it is a 'data driven' design, where you don't repeat blocks of code that perform the same processing, just because a value or where a value comes from changes. you write one block of general processing code and let a data definition, in this case an array, control where the data comes from or what the code does for each possible case.

<?php
// get leave amounts for a particular person, by name
$tool = mysql_query("SELECT *  FROM leave_member WHERE firstname = '$firstname' AND lastname = '$lastname' ");
$fest = mysql_fetch_array($tool);

// map the leave $type to the database column name - these are the only inputs that are dynamic for the processing code
$leave_map['Annual Leave'] = 'annual';
$leave_map['Sick Leave'] = 'sick';
$leave_map['Compassionate Leave'] = 'compassionate';
$leave_map['Study Leave'] = 'study';
$leave_map['Maternity Leave'] = 'maternity';

if(isset($leave_map[$type])){
    $column = $leave_map[$type]; // column name
    $amount = (int)$fest[$column]; // get the amount
    if($amount == 0)
    {
        echo "Sorry, you don't have any $type days available.";
    }
    else if ($workingDays > $amount)
    {
        echo "Sorry, the number of days you are requesting - $workingDays, is more than the number of days you have left - $amount. ";
    } else {
        // you have enough of the requested type
        $query = "INSERT INTO leave_request (
        id, firstname, lastname, department, type, days, startdate, enddate, details, status
        ) VALUES (
        NULL, '$firstname', '$lastname', '$department', '$type','$workingDays', '$startDate', '$enddate','$details', '$status'
        )";
        $sql = mysql_query($query) or die(mysql_error());
    }
} else {
    echo "The requested type - $type, is not valid";
}

this method of coding also helps accomplish DRY (Don't Repeat Yourself), because it lets you eliminate duplicated code.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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