dapcigar Posted October 14, 2014 Share Posted October 14, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291619-multiple-if-statement-not-working/ Share on other sites More sharing options...
Ch0cu3r Posted October 14, 2014 Share Posted October 14, 2014 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/291619-multiple-if-statement-not-working/#findComment-1493531 Share on other sites More sharing options...
Solution mac_gyver Posted October 14, 2014 Solution Share Posted October 14, 2014 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/291619-multiple-if-statement-not-working/#findComment-1493538 Share on other sites More sharing options...
dapcigar Posted October 15, 2014 Author Share Posted October 15, 2014 Thank You.. that solved my problem Quote Link to comment https://forums.phpfreaks.com/topic/291619-multiple-if-statement-not-working/#findComment-1493565 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.