0

嗨,我的代码有问题。我需要从一个选择选项中检索数据,该选项是从 sql 中选择的。这是代码:

<form action="zadany.php" method="get">
    <label for="select">
Komu zadat novej úkol:
        <select name="users">
<?php
$sql = "SELECT * FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
    $id=$row["id"];
    $username=$row["username"];
    $options.="<OPTION VALUE=\"$id\">".$username;
}
?>
    <option>
        <? echo $options ?>
    </option>
</select>

<br />
Typ úkolu:
<table border="1">

<input type="radio" name="ukol" value="bla1">Ukol 1</input>
<input type="radio" name="ukol" value="bla2">Ukol 2</input>
<input type="radio" name="ukol" value="bla3">Ukol 3</input>

</table>
<br />
<input type="submit" value="zadej úkol">

</form>

行动是:

<?php
include 'core/init.php';
protect_page();
include 'includes/overall/header.php';


echo $_GET['users'];

switch ($_GET["ukol"]) {
    case "bla1":
        echo 'ukol 1';
        break;
    case "bla2";
        echo 'ukol 2';
        break;
    case "bla3":
        echo 'ukol 3';
        break;
    default: 
        echo 'nezadany ziadny ukol';
    }
include 'includes/overall/footer.php';
?>

所以我需要例如在选择菜单中回显所选选项。有人可以帮我解决这个问题吗?非常感谢每一个帖子和答案。

4

1 回答 1

0

您的 PHP 代码正在生成以下代码:

<option>
   <OPTION value="your id #1"> Username
</option>

你可以试试:

<select name="users">
<?php
$sql = "SELECT * FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
    $id=$row["id"];
    $username=$row["username"];
    $options.="<OPTION VALUE=\"$id\">".$username."</OPTION>";
}
?>

    <? echo $options ?>

</select>
于 2014-01-06T13:55:47.803 回答