0

我有一个脚本,可以从表中获取所有 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。

4

2 回答 2

0
$selected = ($idfunc)==$db_field['idfunc'] ? ' selected="selected"' : '';
  echo "<option value'".$db_field['idfunc']."' ".$selected." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";

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

于 2013-07-20T12:21:50.700 回答
0

$idfunc->从中删除$idfunc->$db_field['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);

}
?>
于 2013-07-20T12:07:30.553 回答