如何从 MySQL 中提出 PHP 中的 SELECTED 选项?

发布于 2024-08-25 12:40:58 字数 969 浏览 2 评论 0原文

在我的更新表单中,我希望字段能够调用已存储的值。这在文本字段中非常简单,但对于我的下拉菜单 (),我在 PHP 读取已存储的用户名时遇到了问题。这是我的查询和代码:

$sql = "SELECT users.user_id, users.name FROM users";
                $result = mysql_query($sql, $connection)
                or die ("Couldn't perform query $sql <br />".mysql_error());
                $row = mysql_fetch_array($result);?>

        <label>Designated Person:</label> <select name="name" id="name">

        <option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['name']?> - Current</option>

         <?php    
              while($row = mysql_fetch_array($result))
        { ?>                        <option value="<?php echo $row['user_id']; if (isset($_POST['user_id']));?>"><?php echo $row['fullname']?></option>
        <?php } ?>

其结果显示所有用户(根据需要)并让我选择一个用户,然后成功执行更改...但是“SELECTED”始终是我的数据库中的第一个用户,而不是第一个用户添加我的活动时选择的用户:(!!!

In my update form, I want the fields to recall the values that are already stored. This is very simple in a text field, but for my drop down () I'm having trouble with PHP reading the already stored name of user. Here is my query and code:

$sql = "SELECT users.user_id, users.name FROM users";
                $result = mysql_query($sql, $connection)
                or die ("Couldn't perform query $sql <br />".mysql_error());
                $row = mysql_fetch_array($result);?>

        <label>Designated Person:</label> <select name="name" id="name">

        <option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['name']?> - Current</option>

         <?php    
              while($row = mysql_fetch_array($result))
        { ?>                        <option value="<?php echo $row['user_id']; if (isset($_POST['user_id']));?>"><?php echo $row['fullname']?></option>
        <?php } ?>

The result of this displays all of the users (as required) and lets me select a user then perform the change successfully...however the 'SELECTED' is always the first one in my database and never the user that was selected when my activity was added :( !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好倦 2024-09-01 12:40:58

您现在要做的是:

  • 选择第一个结果行并将其作为选项打印出来,并将 SELECTED 硬编码在其中;
  • 循环遍历结果行的其余输出选项;您没有在任何地方检查当前选定的值,但那里有一个空的 if() 没有任何用途;
  • 我看不到当前存储的名称所在的任何地方。

试试这个(假设 $curname 是当前选择的名称):

$sql = "SELECT users.user_id, users.name FROM users";
        $result = mysql_query($sql, $connection)
        or die ("Couldn't perform query $sql <br />".mysql_error());
?>

<label for="name">Designated Person:</label>
    <select name="name" id="name">

 <?php    
      while($row = mysql_fetch_array($result))
{ ?>                        
    <option value="<?php echo $row['user_id'] ?>" <?php if ($curname == $row['name']) echo ' SELECTED'; ?>><?php echo $row['fullname']?></option>
<?php } ?>

What you're doing now is:

  • Selecting the very first result row and printing it out as an option with SELECTED hardcoded in;
  • Looping thru the rest of the results row outputting options; you aren't checking for currently selected value anywhere, but you have an empty if() there for no purpose;
  • I can't see anywhere where the currently stored name is held.

Try this (assuming $curname is the currently selected name):

$sql = "SELECT users.user_id, users.name FROM users";
        $result = mysql_query($sql, $connection)
        or die ("Couldn't perform query $sql <br />".mysql_error());
?>

<label for="name">Designated Person:</label>
    <select name="name" id="name">

 <?php    
      while($row = mysql_fetch_array($result))
{ ?>                        
    <option value="<?php echo $row['user_id'] ?>" <?php if ($curname == $row['name']) echo ' SELECTED'; ?>><?php echo $row['fullname']?></option>
<?php } ?>
弥枳 2024-09-01 12:40:58

嗯...我认为你的代码不完整。例如,最后一个选项有一个不执行任何操作的 if 语句...并且您不应该将所选内容放在同一选择元素中的两个不同选项上。

如果没有其他人,第一个选项总是默认选择的,并且....对于 xhtml 尝试使用 selected="selected" 而不是 SELECTED

另外,您试图回显 $row['fullname'],但您的查询是获取列“名称”

<?php
$sql = "SELECT users.user_id, users.name FROM users";
$result = mysql_query($sql, $connection) or die ("Couldn't perform query $sql <br/>".mysql_error());
$row = mysql_fetch_array($result);
?>

<label>Designated Person:</label> 

<select name="name" id="name">
    <option value="<?php echo $row['user_id']?>"><?php echo $row['name'] ?> - Current</option>
<?php    
while($row = mysql_fetch_array($result)){ ?>
   <option 
      value="<?php echo $row['user_id'] ?>"
      <?php echo (isset($_POST['name']) && $_POST['name'] == $row['user_id']) ? ' selected="selected"' : '');?>
><?php echo $row['name']?></option>
<?php } ?>

Hmm... i think your code is not complete. Ex the last option got an if-statement that does nothing...and you should not put selected on two diffrent options in the same select element.

The first option is always default selected if no-one else is, and.... for xhtml try using selected="selected" instead of SELECTED

Also, you are trying to echo $row['fullname'], but your query is fetching column 'name'

<?php
$sql = "SELECT users.user_id, users.name FROM users";
$result = mysql_query($sql, $connection) or die ("Couldn't perform query $sql <br/>".mysql_error());
$row = mysql_fetch_array($result);
?>

<label>Designated Person:</label> 

<select name="name" id="name">
    <option value="<?php echo $row['user_id']?>"><?php echo $row['name'] ?> - Current</option>
<?php    
while($row = mysql_fetch_array($result)){ ?>
   <option 
      value="<?php echo $row['user_id'] ?>"
      <?php echo (isset($_POST['name']) && $_POST['name'] == $row['user_id']) ? ' selected="selected"' : '');?>
><?php echo $row['name']?></option>
<?php } ?>
挽袖吟 2024-09-01 12:40:58
            <?php
                $result = mysql_query("select (row) from (table)");
                while($row = mysql_fetch_row($result))
                {
                    echo '<option value='.$row[0].'>'.$row[1].'</option>';
                }
            ?>
            <?php
                $result = mysql_query("select (row) from (table)");
                while($row = mysql_fetch_row($result))
                {
                    echo '<option value='.$row[0].'>'.$row[1].'</option>';
                }
            ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文