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
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');
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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