Page 1 of 1

PHP ignoring my IF Statements

Posted: Tue Apr 06, 2010 5:05 pm
by shenjeff

Code: Select all

<?php
function kill() {
  echo "<table width='100%'><tr><td>&nbsp;</td></tr><tr><span class='boldblue'>There has been a problem.<br /><a href='index1.php?page=ivaorfe2010'>Please click here to try book another flight.</a></span><td></td></tr><tr><td>&nbsp;</td></tr></table>";
}

function error() {
    echo "<table width='100%'><tr><td>&nbsp;</td></tr><tr><span class='boldblue'>We are sorry, the flight has already been booked.<br /><a href='index1.php?page=ivaorfe2010'>Please click here to try book another flight.</a></span><td></td></tr><tr><td>&nbsp;</td></tr></table>";
}

function book($pilotid, $flightno) {
    mysql_query("UPDATE ivaorfetmp SET pilot_id='$pilotid' WHERE flightno='$flightno'");
    echo "<table width='100%'><tr><td>&nbsp;</td></tr><tr><span class='boldblue'>Your booking has been submitted successfully, we hope you enjoy your flight.<br />You will be redirected to your 'My Bookings' page in 3 seconds...</span><td></td></tr><tr><td>&nbsp;</td></tr></table>";
    echo "<script type='text/javascript'>";
    echo "setTimeout(\"window.location.href = 'index.php?page=my_booked_flights';\" , 3000);";
    echo "</script>";
}

if (isset($_GET['flightid'])) {
$flightno = $_GET['flightid'];
$pilotid = $flyuk->pilotid;

$avail = $flyuk->db_query("SELECT pilot_id FROM ivaorfetmp WHERE flightno='$flightno'");
$booked = mysql_result($avail,0);

if (($booked !='')||(!empty($booked))) {
  error();
} else {
  book($pilotid, $flightno);
}

} else {
kill();
}
?>
I have the code above. PHP gives me the results of the function error but also runs the mysql_query in the book function. I tried doing it without the functions and putting the code directly within the If function and it does the same. I have spent the past 4 hours trying to figure out why it runs both parts of the If... Else... statement. Can someone please help me before I crack my skull. :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:

Thank you very much in advance!

Re: PHP ignoring my IF Statements

Posted: Tue Apr 06, 2010 6:37 pm
by Christopher
PHP is not ignoring you if statements ... it is doing (and always will do) exactly what you are telling it to do. That's what you need to figure out:

Code: Select all

$avail = $flyuk->db_query("SELECT pilot_id FROM ivaorfetmp WHERE flightno='$flightno'");
$booked = mysql_result($avail,0);
What is $flyuk and what is its query method returning? Have you checked for errors? Are there any records in the result set? Maybe you only need to call mysql_ num_ rows() instead of mysql_result()? Is this logic "($booked !='')||(!empty($booked)" really the check for an error on the first field of the first record in the result set?

Re: PHP ignoring my IF Statements

Posted: Tue Apr 06, 2010 6:44 pm
by shenjeff
basically its the UPDATE query within the book function that is running with everything in the error function.

($booked !='')||(!empty($booked)) is used to test whether the first MySQL statement returns a value or not. If it does, then I want it to throw out everything in the error function, otherwise go ahead and update the field. The $flyuk->db_query basically is a class set which runs the mysql_query but also records the query run for error reporting in debug mode. I have echoed the result of the SELECT statement and it does run properly.

The part of the script that is breaking down is when I, in simple terms ask

If (FALSE) {
function a
} else {
function b
}

but it runs both function a and function b for some odd reason.

Thank you very much,