...

View Full Version : A few questions - abit stuck



Gez
05-14-2008, 03:13 PM
Ok, my code is below but i can't seem to get anything thats going on in the functions ShowRetFlights and ShowDepFlights to appear/work i think it might be something to do with the sessions but im not sure. Also if anyone knows, how should i go about getting the text boxes in the form to keep the vale that was typed in before pressing the submit button?

Thanks in advance


<style type="text/css">
<!--
body {
background-image: url(bg.jpg);
text-align: center
}
body,td,th {
font-family: Arial, Helvetica, sans-serif;
}
h1 {
font-size: xx-large;
}
-->
</style>
<?php

session_start();

Include ("dbconnect.php");

Function form(){
echo "
<form action='".$_SERVER['PHP_SELF']."' method='GET'>
<table align='center' width='200' border='0'>
<tr>
<td align='center'>Destination:</td>
<td align='center'>Flying from:</td>
</tr>
<tr>
<td><input name='destination' type='text'></td>
<td><input name='flyfrom' type='text'></td>
</tr>
<tr>
<td align='center'>Date (YYYY-MM-DD)</td>
<td align='center'>Date (YYYY-MM-DD)</td>
</tr>
<tr>
<td><input name='depdate' type='text'></td>
<td><input name='retdate' type='text'></td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' value='Take me there!'></td>
</tr>
</table>
</form>";
}

Function ShowRetFlights (){
// Set Sessions
$DestinationName=$_REQUEST['destination'];
$FlyingFrom=$_REQUEST['flyfrom'];
$RetDate=$_REQUEST['retdate'];
// Do the query
$resultret = mysql_query("SELECT * FROM $table_flight
WHERE DestinationAirport ='".$FlyingFrom."'
AND DepartureDate ='".$RetDate."'
AND DepartureAirport ='".$DestinationName."'");

return ($resultret);

echo " <b> INBOUND flights </b> <br/><br/>
<table align='center' width='200' border='0'>
<tr>
<td align='center'>Destination: </td>
<td align='center'>Airline:</td>
<td align='center'>Departure Airport:</td>
<td align='center'>Departure Date:</td>
<td align='center'>Departure Time:</td>
<td align='center'>Cost:</td>
<td align='center'></td>
</tr>";

// Display the results
while ($row = mysql_fetch_array( $resultret )){
echo "<tr>";
echo " <td align='center'>".$row['DestinationAirport']."</td>";
echo " <td align='center'>".$row['Airline']."</td>";
echo " <td align='center'>".$row['DepartureAirport']."</td>";
echo " <td align='center'>".$row['DepartureDate']."</td>";
echo " <td align='center'>".$row['DepartureTime']."</td>";
echo " <td align='center'>".$row['Fare']."</td>";
echo " <td align='center'></td>";
echo " </tr>";
}
echo "</table><br/><hr><br>/";
}



Function ShowDepFlights (){
$table_flight="flight";
// Set Sessions
$DestinationName=$_REQUEST['destination'];
$DepDate=$_REQUEST['depdate'];
$FlyingFrom=$_REQUEST['flyfrom'];
$RetDate=$_REQUEST['fretdate'];

// Do the query
$resultdep = mysql_query("SELECT * FROM $table_flight
WHERE DestinationAirport ='".$DestinationName."'
AND DepartureDate ='".$DepDate."'
AND DepartureAirport ='".$FlyingFrom."'");

return ($resultdep);

echo " <b> OUTBOUND flights </b> <br/><br/>
<table width='200' border='1'>
<tr>
<td align='center'>Destination: </td>
<td align='center'>Airline:</td>
<td align='center'>Departure Airport:</td>
<td align='center'>Departure Date:</td>
<td align='center'>Departure Time:</td>
<td align='center'>Cost:</td>
<td align='center'></td>
</tr>";

// Display the results
while ($row = mysql_fetch_array( $resultdep )){
echo "<tr>";
echo " <td align='center'>".$row['DestinationAirport']."</td>";
echo " <td align='center'>".$row['Airline']."</td>";
echo " <td align='center'>".$row['DepartureAirport']."</td>";
echo " <td align='center'>".$row['DepartureDate']."</td>";
echo " <td align='center'>".$row['DepartureTime']."</td>";
echo " <td align='center'>".$row['Fare']."</td>";
echo " <td align='center'></td>";
echo " </tr>";
}
echo "</table><br/><hr><br>/";
}


echo "
<html>
<head>
<title> Lufbra Travel </title>
</head>
<body>
<h1>Lufbra Travel</h1> ";

form();

ShowDepFlights();

ShowRetFlights();

echo "
<hr><br/>
&copy; Geraint Fisher 2008
</body>
</html> ";

?>

RMcLeod
05-14-2008, 03:28 PM
By the looks of it I would say remove these two lines:
return ($resultret); and return ($resultdep);
These are ending your functions prematurely and returning the query result identifier, which isn't needed.

I would recommend setting the form method to "POST" instead of "GET" and using $_POST instead of $_REQUEST (this won't fix your problem, but it's good practice).

To get form fields to keep their submitted values.


<input type="text" name="destination" value="<?php echo isset($_POST['destination']) ? $_POST['destination'] : ''; ?>" />


Note that this will only work if you take my recommendation to swap all of your $_REQUEST, to $_POST, otherwise replace $_POST in my code snippet with $_REQUEST.

Also I would recommend opening your strings with ' instead of ", this allows you to use " in your html tags. Or better yet use HEREDOC syntax.

Gez
05-14-2008, 04:10 PM
thank you RMcLeod that fixed my original problem but now i have the error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /disks/diskh/zco/cogf/public_html/Planner/flightfinder.php on line 74

which is
while ($row = mysql_fetch_array( $resultdep )){

RMcLeod
05-14-2008, 04:19 PM
I'm guessing there's an error in your sql, add or die(mysql_error()) to the end of your sql query to find out what it is.

Like This:


<?php
$resultdep = mysql_query("SELECT * FROM $table_flight
WHERE DestinationAirport ='".$DestinationName."'
AND DepartureDate ='".$DepDate."'
AND DepartureAirport ='".$FlyingFrom."'") or die (mysql_error());
?>

Gez
05-14-2008, 04:32 PM
Yup, ive done that and i get the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE DestinationAirport ='Gatwick' AND DepartureDate ='2008-01-08' AND ' at line 2

i can't see the problem when i compare it to the one that does actully work :/

RMcLeod
05-14-2008, 04:36 PM
I can't see anything wrong with that bit of SQL, replace the 'or die(mysql_error());' with 'or die ($resultdep);' This will give you the entire SQL query as passed to the server, see if you can see any errors in it.

Gez
05-14-2008, 04:47 PM
I did that and nothing atall came up, thanks alot anyway.

RMcLeod
05-14-2008, 04:52 PM
My mistake of course it won't. This will work



<?php
$resultdep = "SELECT * FROM $table_flight
WHERE DestinationAirport ='".$DestinationName."'
AND DepartureDate ='".$DepDate."'
AND DepartureAirport ='".$FlyingFrom."'";

mysql_query($resultdep) or die ($resultdep);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum