Jump to content

PHP website script problem


w3rthlesspe0n

Recommended Posts

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: webair1.jpg

And if they put in a selection that isn't in the database they get this:

webair2.jpg

 

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:

webair3.jpg

 

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?  :confused:

 

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 &pound';
echo $row["price"];
}
else include('includes/price.php');
?>
</div> 

 

My if-else statement doesn't seem to be doing it's job  :shrug:

 

Thanks in advance ;D

Link to comment
https://forums.phpfreaks.com/topic/232076-php-website-script-problem/
Share on other sites

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 &pound';
	echo $row["price"];
}
else include('includes/price.php');
}
?>

Archived

This topic is now archived and is closed to further replies.

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