Jump to content

I Can't Create Dynamic Drop Down.


perseas

Recommended Posts

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!

Edited by perseas
Link to comment
Share on other sites

Where were you trying to declare $link as a global variable? Directly before you used it, within the renderForm() function?

 

Realistically though... at least, according to the way I had organized my code... that renderForm should do just that -- render the form. Personally, I would access the database and retrieve the data you need outside of the form, store that data in an array, and pass it to the renderForm() function. This way, the code is a little cleaner, in the sense that you are separating the logic of the page from the HTML/CSS that gets displayed.

Link to comment
Share on other sites

Where were you trying to declare $link as a global variable? Directly before you used it, within the renderForm() function?

 

Realistically though... at least, according to the way I had organized my code... that renderForm should do just that -- render the form. Personally, I would access the database and retrieve the data you need outside of the form, store that data in an array, and pass it to the renderForm() function. This way, the code is a little cleaner, in the sense that you are separating the logic of the page from the HTML/CSS that gets displayed.

 

I declare $link as global, over the mysql_connect.

e.g.

global $link;

$link = mysql_connect( $hostname , $username , $password ) or

die("Attention! Server Connection Problem : " . mysql_error());

 

I'll post the code in which the access and the data are retrieved outside the form, and stored in an array to the function. Soon!

Link to comment
Share on other sites

That's not the way $global works though. See http://php.net/manual/en/language.variables.scope.php

 

I believe if you include the line

 

global $link;

 

within your renderForm function, right before you use it, it should work fine.

 

$global link; added in the code. Only one warning, but nothing happened. I'm sending in a few minutes the code you asked for...(retrieve data outside the renderform)

PHP Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\haa\ben.php on line 26

Link to comment
Share on other sites

The new code. the data are retrieved (succesfully) outside the renderform, and i pass them inside the function.

 

 

<?php //add_flight.php

session_start();

$hostname = "myhostname";

$database = "mydatabase";

$username = "username";

$password = "password";

$link = mysql_connect( $hostname , $username , $password ) or die("Attention! Server Connection Problem : " . mysql_error());

mysql_select_db($database,$link);

?>

 

<?php

$i=0;

$result = mysql_query("SELECT * FROM users",$link);

while($row = mysql_fetch_array($result))

$array[$i]=$row['name'];

 

//for ($k=0;$k<=$i;$k++) echo $array[$k]; // I can see the content of $array. It's right.

?>

 

 

 

 

<?php

function renderForm($fdate, $pic, $planetype,$link, $array,$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>';

}

?>

<?/*php $result = mysql_query("SELECT * FROM users",$link)or die(mysql_error());

$row == mysql_fetch_array($result);

echo $row[name];*/?>

 

<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

$count=count($array);

for ($k=0;$k<$count;$k++)?>

<option value="<?php $array['$k']?>"><?php $array['$k']?></option> //// line 48

 

 

?>

</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,$link, $array,$error);

}

else

{

mysql_query("INSERT flights SET fdate='$fdate', pic='$pic', planetype='$planetype'") or die(mysql_error());

 

header("Location: add_flight.php");

}

}

else

{

renderForm('','','', '','', '');

}

?>

 

error log

[14-Oct-2013 18:52:22 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 48

 

[14-Oct-2013 18:52:22 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 48

Edited by perseas
Link to comment
Share on other sites

First thing I notice: Where is $row['$k'] being set within the renderForm() function? I see where it's intended to be set, but it looks like it has been commented out? Is that supposed to be $row?

 

Secondly, why is there any need to pass around the number of elements in the array? This likely doesn't have anything to do with your error, but it's just adding unnecessary complexity to your code. For example, you could simplify the retreival of the data:

 

<?php

$result = mysql_query("SELECT * FROM users",$link);

while($row = mysql_fetch_array($result))

$array[]=$row['surname'];

}

//for ($k=0;$k<count($array);$k++) echo $array[$k]; // I can see the content of $array. It's right.

?>

 

Then, you can do the same thing with the renderForm() function, using count(), and there's no need to pass around $i.

Link to comment
Share on other sites

First thing I notice: Where is $row['$k'] being set within the renderForm() function? I see where it's intended to be set, but it looks like it has been commented out? Is that supposed to be $row?

 

Secondly, why is there any need to pass around the number of elements in the array? This likely doesn't have anything to do with your error, but it's just adding unnecessary complexity to your code. For example, you could simplify the retreival of the data:

 

<?php

$result = mysql_query("SELECT * FROM users",$link);

while($row = mysql_fetch_array($result))

$array[]=$row['surname'];

}

//for ($k=0;$k<count($array);$k++) echo $array[$k]; // I can see the content of $array. It's right.

?>

 

Then, you can do the same thing with the renderForm() function, using count(), and there's no need to pass around $i.

 

 

Ι corrected the code with the count command. Αnd I edited the previous post with my code.

For your first notice. I didn't undesrtand. I have already the $array with the data. Is it necessary to have a mysql_query again? This is why I commented out.

about the $row['$k'], it was my mistake, I corrected it.

Link to comment
Share on other sites

For this line:

 

for ($k=0;$k<=$count;$k++)?>

You need to use:

 

for ($k=0;$k<$count;$k++)?>

(Note the "<" not "<=". This is because count() returns a whole number, but arrays start at key 0. So if count() returns 3, you only have three array items, and you'd need to loop array items 0-2.

 

No, you don't need to have another mysql_query. I was just confused by your $row / $array typo.

Link to comment
Share on other sites

For this line:

 

for ($k=0;$k<=$count;$k++)?>

You need to use:

 

for ($k=0;$k<$count;$k++)?>

(Note the "<" not "<=". This is because count() returns a whole number, but arrays start at key 0. So if count() returns 3, you only have three array items, and you'd need to loop array items 0-2.

 

No, you don't need to have another mysql_query. I was just confused by your $row / $array typo.

Thanks for the correction, but still the list is empty. I can't understand why..

Link to comment
Share on other sites

Still getting errors? Or no errors any more, but the dropown list doesn't have any items in it?

 

The drop down list is empty. And the error log has two notices

[14-Oct-2013 18:59:37 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 51

 

[14-Oct-2013 18:59:37 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 51

 

pointing this command:

 

<option value="<?php $array[$k]?>"><?php $array[$k]?></option>

 

(all the code is updated at the previous post)

Link to comment
Share on other sites

Why don't you do this... Can you send me a backup of your database (I imagine you should be able to use PHPMyAdmin's Export functionality?) and the file you are working with, I'll be able to experiment a little and figure things out. At the moment, I feel like I'm just doing guesses and it'll be much quicker if I can play with the code directly. ben [at] falkencreative.com

Link to comment
Share on other sites

Figured out the problem. Sorry I didn't see this earlier, but that's why I prefer to work with actual code rather than posted code -- it's much easier to debug and figure out what's going on.

 

#1: You never actually passed $array into the renderForm() function. You were passing an empty string in, causing issues. So at the end of the file, you'd want to change:

 

renderForm('','','', '','', '');

 

to

 

renderForm('','','', '',$array, '');

 

#2: You can't simply reference the variables, you need to echo them out, so replace:

 

<option value="<?php $array[$k]?>"><?php $array[$k]?></option>

 

with

 

<option value="<?php echo $array[$k]?>"><?php echo $array[$k]?></option>

 

#3: You need brackets around your for loop. So instead of

 

<?php

$count=count ($array);

for ($k=0;$k<$count;$k++) { ?>

<option value="<?php echo $array[$k]?>"><?php echo $array[$k]?></option> <?php } ?>

 

you need to use

 

<?php

$count=count ($array);

for ($k=0;$k<$count;$k++) { ?>

<option value="<?php echo $array[$k]?>"><?php echo $array[$k]?></option> <?php } ?>

 

Basically, the way the PHP ends and changes to HTML causes some confusion within PHP -- it doesn't realize it's supposed to loop the next line. The loop runs without doing anything, and $k ends up being $count, and thus when the <option> line runs, it tries to process $array[$k], an array item that doesn't exist, causing the error.

 

Hope that all makes sense?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...