w3rthlesspe0n Posted March 29, 2011 Share Posted March 29, 2011 So I'm working on a website for an airline (not real) and on one of my pages you can request the cost of a flight by putting in the depart and arrival locations, like this: And if they put in a selection that isn't in the database they get this: The problem is that when they load the page initially, instead of just the selection bars and sumbit button showing up, it all does and i get an error like this: When you sumbit it, it all looks like it should, it's just that when they load it things seem to go wrong, any idea how i can fix this? Heres the code i use: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>Depart: <select name="depart"> <option selected="selected">Bristol</option> <option>Newcastle</option> <option>Manchester</option> <option>Dublin</option> <option>Glasgow</option> </select> Arrive: <select name="arrive"> <option>Bristol</option> <option selected="selected">Newcastle</option> <option>Manchester</option> <option>Dublin</option> <option>Glasgow</option> </select> <input type="submit" /> </p> </form> <?php $depart = $_POST['depart']; $arrive = $_POST['arrive']; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT price FROM flights WHERE depart = '$depart' AND arrive = '$arrive'"); if ($row = mysql_fetch_assoc($result)) { echo 'This flight shall cost you £'; echo $row["price"]; } else include('includes/price.php'); ?> </div> My if-else statement doesn't seem to be doing it's job Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/232076-php-website-script-problem/ Share on other sites More sharing options...
Cagecrawler Posted March 29, 2011 Share Posted March 29, 2011 Assuming that price.php gives you the blue table, you should add an if around the db stuff to only process when the form is submitted: <?php if(isset($_POST['depart'])) { $depart = $_POST['depart']; $arrive = $_POST['arrive']; //... All db connection/query stuff if ($row = mysql_fetch_assoc($result)) { echo 'This flight shall cost you £'; echo $row["price"]; } else include('includes/price.php'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232076-php-website-script-problem/#findComment-1193764 Share on other sites More sharing options...
w3rthlesspe0n Posted March 29, 2011 Author Share Posted March 29, 2011 Worked cheers man Quote Link to comment https://forums.phpfreaks.com/topic/232076-php-website-script-problem/#findComment-1193769 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.