PDA

View Full Version : PHP-Beginner - Probs with Dropdown


RayMatrix
01-01-2003, 12:42 PM
Hi,

is there a way to show the results of an 'select' event in dropdownmenues ?

All Hints or Codesamples are welcome

Thanks for your help !!

Regards

GSHelpBoy
01-01-2003, 04:36 PM
Sure.<?php
// connect
// select db
// sql query
$query = mysql_query($sql);
$i = 0;
while ($row = mysql_fetch_array($query)) {
print('<option value="' . $row[$i] . "'>' . $row[$i] . '</option>');
$i++;
}
?>

RayMatrix
01-01-2003, 06:23 PM
But it wont work at my side.

This is my Code:
<?
include ("config.php"); // connect to database
$result=mysql_query("SELECT mitarbeiter_betreuer FROM mitarbeiter");
$i = 0;
while ($row = mysql_fetch_array($result));
{
print('<option value='" . $row[$i] . "'>' . $row[$i] . '</option>');
$i++;
}
mysql_close();
?>But i get an Parse error at the line:
print('<option value='" . $row[$i] . "'>' . $row[$i] . '</option>');any Idea ?

RayMatrix
01-01-2003, 06:51 PM
. . .in that line, but it wont work also.

This is the hole Code now:
<!-insert.php-> // Name of File
<html>
<body>
<?
if ($button==" OK ") {
include ("config.php"); // connect to the Database
mysql_query("INSERT INTO mitarbeiter(mitarbeiter_vorname,mitarbeiter_name,m itarbeiter_nummer,mitarbeiter_betreuer)VALUES('$vo rname','$name','$nummer','$betreuer')");

mysql_close();
?>
<p>Die Daten wurden eingetragen<br>
<a href="insert.php">neuer Eintrag</a><br>
<a href="fetcharr.php">zur ?bersicht</a>
<?
}
else {
?><form method="post" action="insert.php">
<input type="text" name="vorname" value="vorname">
<br>
<input type="text" name="name" value="name">
<br>
<input type="text" name="nummer" value="nummer">
<br>
<?
include ("config.php"); // connect to the Database
$result=mysql_query("SELECT mitarbeiter_betreuer FROM mitarbeiter");
$i = 0;
while ($row = mysql_fetch_array($result));
{
print('<option value=' . "$row[$i]" . '>' . $row[$i] . '</option>');
$i++;
}
mysql_close();
?>
<br><input type="submit" name="Submit" value="Abschicken">
</form>
</body>
</html>But now it tells me an Parseerror in the last line. . .

RayMatrix
01-01-2003, 09:22 PM
. . .my mistake.

The Code do not bring up any more errors but now there are no entries in the dropdown :(

here the Code again:
<!-insert.php->
<html>
<body>
<?
if ($button==" OK ") {
include ("config.php");
mysql_query("INSERT INTO mitarbeiter(mitarbeiter_vorname,mitarbeiter_name,m itarbeiter_nummer,mitarbeiter_betreuer)VALUES('$vo rname','$name','$nummer','$betreuer')");

mysql_close();
?>
<p>Die Daten wurden eingetragen<br>
<a href="insert.php">neuer Eintrag</a><br>
<a href="fetcharr.php">zur ?bersicht</a>
<?
}
else {
?><form method="post" action="insert.php">
<input type="text" name="vorname" value="vorname">
<br>
<input type="text" name="name" value="name">
<br>
<input type="text" name="nummer" value="nummer">
<br>
<select name="select">
<?
include ("config.php");
$result=mysql_query("SELECT mitarbeiter_betreuer FROM mitarbeiter");
echo $result;
$i = 0;
while ($row = mysql_fetch_array($result));
{
print('<option value="' . $row[$i] . '">' . $row[$i] . '</option>');
$i++;
}
mysql_close();
?>
</select>
<br><input type="submit" name="Submit" value="Abschicken">
</form>
<?
}
?>
<form name="form1" method="post" action="">
<select name="select">
<option value="1">test</option>
</select>
</form>
</body>
</html>

GSHelpBoy
01-02-2003, 12:57 AM
Does config.php connect to the database, because otherwises, you will not get any result.

RayMatrix
01-02-2003, 02:35 PM
. . .i got it now to work.

There were some little Errors in your Code ;)
Here is the one wich works fine for me:
<!-insert.php->
<html>
<body>
<?
if ($button==" OK ") {
include ("config.php");
mysql_query("INSERT INTO mitarbeiter(mitarbeiter_vorname,mitarbeiter_name,m itarbeiter_nummer,mitarbeiter_betreuer)VALUES('$vo rname','$name','$nummer','$betreuer')");
$result=mysql_query("SELECT * FROM mitarbeiter");
//echo mysql_num_rows($result);
mysql_close();
?>
<p>Die Daten wurden eingetragen<br>
<a href="insert.php">neuer Eintrag</a><br>
<a href="fetcharr.php">zur ?bersicht</a>
<?
}
else {
?>
<form method="post" action="insert.php">
<input type="text" name="vorname" value="vorname">
<br>
<input type="text" name="name" value="name">
<br>
<input type="text" name="nummer" value="nummer">
<br>
<select name="betreuer">
<?
include ("config.php");
$result=mysql_query("SELECT mitarbeiter_name FROM mitarbeiter");
// echo mysql_num_rows($result);
$i = 0;
while ($row = mysql_fetch_array($result))
{
print('<option value="' . $row[$i] . '">' . $row[$i] . '</option>');
// $i++;
}
mysql_close();
?>
</select>
<br><input type="submit" name="button" value=" OK ">
</form>
<?
}
?>
</body>
</html>

Big Thanks again, your Hints were great !!