在while内部选择的属性



我有一个脚本,可以从一个表中获取所有id,并将它们打印在选项选择窗体上,我想用我在选项中选择的id重新加载页面。这是脚本:

<?php
include('include/menu.php');
include('include/mysql.php');
if ($db_found) {
echo "<form action='' name='form' method ='get'>
<select name='funcionario'>";
        $SQL = "SELECT * FROM funcionarios";
        $result = mysql_query($SQL);
        while ( $db_field = mysql_fetch_assoc($result) ) {
$idfunc = $_GET['funcionario'];
$selected = ($idfunc==$idfunc->$db_field['idfunc']) ? ' selected="selected"' : '';
                echo "<option value'".$db_field['idfunc']."' ".$select." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";
        }
        echo "</selected></form>";
        echo $idfunc;
} else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>

但是脚本总是只返回选定的第一个id。

$idfunc->$db_field['idfunc'] 中删除$idfunc->

value='".$db_field['idfunc']."' 替换value'".$db_field['idfunc']."'

您已经定义了$idfunc = $_GET['functionario'],它是一个字符串,而不是类对象。

此外,您定义了$selected,但在回显结果时使用了$select

要进行进一步的调试,请在脚本顶部使用error_reporting(E_ALL);。我想这就是为什么您在尝试执行这个脚本时没有出错的原因。

以下是完整的脚本:

<?php
include('include/menu.php');
include('include/mysql.php');
if ($db_found) {
echo "<form action='' name='form' method ='get'>
<select name='funcionario'>";
        $SQL = "SELECT * FROM funcionarios";
        $result = mysql_query($SQL);
        while ( $db_field = mysql_fetch_assoc($result) ) {
          $idfunc = $_GET['funcionario'];
          $selected = ($idfunc==$db_field['idfunc']) ? ' selected="selected"' : '';
          echo "<option value='".$db_field['idfunc']."' $selected onclick='document.form.submit();'>".$db_field['nomefunc']."</option>";
        }
        echo "</selected></form>";
        echo $idfunc;
} else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
$selected = ($idfunc)==$db_field['idfunc'] ? ' selected="selected"' : '';
  echo "<option value'".$db_field['idfunc']."' ".$selected." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";

我希望这是你想要的!试试这个!

最新更新