Jump to content


Photo

Validating against another field?


  • Please log in to reply
2 replies to this topic

#1 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 09 October 2006 - 07:17 PM

Ok the below script does the following.  From a form it inserts the total passengers into the field bPAX. 

<?

if(isset($_POST['update']))
{

$total_pax = addslashes(trim($_POST['total_pax']));

include 'db.php';

$sql = "SELECT * FROM routes WHERE flight_id = '$flight_id'";

$result = mysql_query($sql);

$routes_array = mysql_fetch_array($result);

//add pax to bPAX
$bPAX = $routes_array['bPAX'];
$newtotal = $bPAX + $total_pax;


mysql_query("UPDATE routes SET bPAX = '$newtotal' WHERE flight_id = '$flight_id'");

mysql_close($connection);
}

echo 'YOUR BOOKING HAS BEEN PROCESSED';
}
?>
The problem I am having is that I also have a field "PAX" which is the total capacity for the aircraft say 212.  So for example when somebody submits the form with say 215 passengers it will still add that to the bPAX field (which I don't want). How would I write up a script or an ‘if’ statement so that if the total capacity for the aircraft is 212 (PAX field) and somebody enters 215 (for the bPAX field) it will not let insert that many into the database.  Maybe some sort of check against the both field? 

Cheers


#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 09 October 2006 - 07:33 PM

And if bpax is larger than pax insert to bpax the value of pax (option1) or output an error(option2)?

<?php

if(isset($_POST['update']))
{

$total_pax = addslashes(trim($_POST['total_pax']));

include 'db.php';

$sql = "SELECT * FROM routes WHERE flight_id = '$flight_id'";

$result = mysql_query($sql);

$routes_array = mysql_fetch_array($result);

//add pax to bPAX
$bPAX = $routes_array['bPAX'];
$newtotal = $bPAX + $total_pax;

if($newtotal <= $routes_array['PAX'])
 mysql_query("UPDATE routes SET bPAX = '$newtotal' WHERE flight_id = '$flight_id'");
else
 //Option 1- mysql_query("UPDATE routes SET bPAX = '$routes_array['PAX']' WHERE flight_id = '$flight_id'");
 //Option 2- die("bPAX cant be bigger than pax");


mysql_close($connection);
}

echo 'YOUR BOOKING HAS BEEN PROCESSED';
}
?>

Think you're smarty?

(Gone until 20 to November)

#3 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 09 October 2006 - 08:06 PM

Yes output an error

Thank you very much Orio  :)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users