Hello all! I'm new in PHP and I'm trying to create a dynamic drop-down list. I have two tables, pilots and flights. So, when I want to insert a flight, I'd like to select from a dynamic list with the pilots (i can add pilots, with another php file). But, when I'm trying to run the code, nothing happens. Here is my code:
<?php //add_flight.php
session_start();
$hostname = "myhostname";
$database = "mydatabase";
$username = "myusername";
$password = "mypassword";
$link = mysql_connect( $hostname , $username , $password ) or die("Attention! Server Connection Problem : " . mysql_error());
mysql_select_db($database,$link);
?>
<?php
function renderForm($fdate, $pic, $planetype, $error)
{
?>
<html>
<head>
<title>Add New Flight</title>
</head>
<body>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="add_flight.php" method="post">
<table>
<tr><td><B>Date:</B></td><td><input type="text" name="fdate"value="<?php echo $fdate; ?>"/></td></tr>
<tr><td><B>PIC:</B></td><td><select name="pic"value="<?php echo $pic; ?>"/>
<?php
global $link;
$result = mysql_query("SELECT * FROM pilots",$link)or die(mysql_error());
while($row == mysql_fetch_array($result))
{?>
<option value="<?php $row['pname']?>"><?php $row['pname']?></option>
<?php }?>
</select>
</td></tr>
<tr><td><B>planetype:</B></td><td><select name="planetype"value="<?php echo $planetype; ?>"/>
<option value="cessna">cessna</option>
<option value="piper">piper</option>
<option value="diamond">diamond</option>
</select>
</td></tr>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
}
if (isset($_POST['submit']))
{
$fdate = mysql_real_escape_string(htmlspecialchars($_POST['fdate']));
$planetype = mysql_real_escape_string(htmlspecialchars($_POST['planetype']));
$pic = mysql_real_escape_string(htmlspecialchars($_POST['pic']));
if ($fdate == '' || $pic == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($fdate, $pic, $planetype, $error);
}
else
{
mysql_query("INSERT flights SET fdate='$fdate', pic='$pic', planetype='$planetype'") or die(mysql_error());
header("Location: add_flight.php");
}
}
else
{
renderForm('','','', '');
}
?>
Here is the php_error_log:
PHP Notice: Undefined variable: link in C:\xampp\htdocs\haa\ben.php on line 32
PHP Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\haa\ben.php on line 32
- I also tried the same code with a second function which retrieves succesfully the data outside the renderform and i passed them in renderform. No luck.
- I declared $link as a global variable (global $link), nothing happened, but the notice of the error log stayed on.
- I abbrogated the renderford and modified the code. I can see the data outside the form, but not inside.
Thanks in advance!