从选择标记中获取数据库中的值HTML PHP MySQL [重复]

时间:2022-06-16 09:12:58

This question already has an answer here:

这个问题在这里已有答案:

I'm beginner in PHP MySQL. I successfully get the value from database in INPUT type but I can't get the data from database in SELECT type:

我是PHP MySQL的初学者。我成功从INPUT类型的数据库中获取值,但我无法从SELECT类型的数据库中获取数据:

Here is my sample edit form where Gender & User Type can't output the value from my database:

这是我的示例编辑表单,其中Gender&User Type无法从我的数据库输出值:

从选择标记中获取数据库中的值HTML PHP MySQL [重复]

and here is data from the table I created: 从选择标记中获取数据库中的值HTML PHP MySQL [重复]

这是我创建的表中的数据:

I'm calling the data in my input box by using this code:

我使用以下代码调用输入框中的数据:

<?php $userRow['agentFname']; ?>  //$userRow['ROW NAME IN TABLE'];

But I can't call the data where In select box, like Gender: and User Type here is my code of Select box.

但我无法调用数据在选择框中,如性别:这里的用户类型是我的选择框代码。

   <select class="form-control" name="aGender" >
           <option selected disabled>*Gender</option>
          <option>Male</option>
          <option>Female</option>

   </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

   <select class="form-control" name="utype" >
           <option selected disabled>*User Type</option>
          <option>Agent</option>
          <option>Team Leader</option>
          <option>Admin</option>
        </select>

4 个解决方案

#1


1  

@Edmhar has a good answer, but it is hard coded and isn't good if you want to add more genders (transgenders and what not).

@Edmhar有一个很好的答案,但它是硬编码的,如果你想添加更多的性别(变性人和不是),它是不好的。

What I do is something like this:

我做的是这样的:

<?php
$array = array("male", "female", "other");

echo "<select class='form-control' name='aGender' >";
foreach ($array as $gender) {
    if ($gender == $databaseValue) {
       echo "<option selected>$gender</option>";
    } else {
       echo "<option>$gender</option>";
    }
}
echo "</select>";
?>

Also, don't use disabled on form elements; use read-only. It does the same thing as disabled visually, but disabled does what it says. It blocks the value from being submitted to the database. read-only just prevents editing, but doesn't cause form submission problems. User type will follow the same suit.

另外,不要在表单元素上使用disabled;使用只读。它与视觉上的残疾做同样的事情,但残疾人做了它所说的。它阻止了提交给数据库的价值。只读只是阻止编辑,但不会导致表单提交问题。用户类型将遵循相同的诉讼。

#2


0  

I already have my own answer to this Here how I solved this:

我已经有了自己的答案在这里我是如何解决这个问题的:

<select class="form-control" name="aGender" >
     <?php
       if ($userRow['agentGender'] == 'Male') {

           echo "<option selected>Male</option>";
           echo "<option>Female</option>";

       } else {
           echo "<option selected>Female</option>";
           echo "<option>Male</option>";
       }
    ?>
</select>

#3


0  

I think you can use below conditional code for this issue:

我认为你可以使用下面的条件代码来解决这个问题:

<?php if($userRow['aGender'] == 'Male'){
 $selectedMale =  'selected="selected"'
 $selectedFemale =  ''
}else{
  $selectedFemale =  'selected="selected"'
 $selectedMale =  ''
}?>

<select class="form-control" name="aGender" >
           <option selected disabled>*Gender</option>
          <option <?php echo $selectedMale ;?>>Male</option>
          <option <?php echo $selectedFemale;?>>Female</option>

   </select>

and same for the another select box.

和另一个选择框相同。

It will work hopefully.

它会有希望地工作。

#4


0  

In my opinion it's best to use ternary operator in such a case, combined with options element fetched from db/array. Something like:

在我看来,最好在这种情况下使用三元运算符,并结合从db / array中获取的options元素。就像是:

    <?php $options = Array(1 => "Option 1", 2 => "Option 2")
    <select>
    <?php foreach($options as $key => $opt): ?>
        <option value="<?= $key ?>" ($key == $userRow['value'] ? "selected" : "" )><?= $opt ?></option>
    <?php endforeach; ?>
    </select>

#1


1  

@Edmhar has a good answer, but it is hard coded and isn't good if you want to add more genders (transgenders and what not).

@Edmhar有一个很好的答案,但它是硬编码的,如果你想添加更多的性别(变性人和不是),它是不好的。

What I do is something like this:

我做的是这样的:

<?php
$array = array("male", "female", "other");

echo "<select class='form-control' name='aGender' >";
foreach ($array as $gender) {
    if ($gender == $databaseValue) {
       echo "<option selected>$gender</option>";
    } else {
       echo "<option>$gender</option>";
    }
}
echo "</select>";
?>

Also, don't use disabled on form elements; use read-only. It does the same thing as disabled visually, but disabled does what it says. It blocks the value from being submitted to the database. read-only just prevents editing, but doesn't cause form submission problems. User type will follow the same suit.

另外,不要在表单元素上使用disabled;使用只读。它与视觉上的残疾做同样的事情,但残疾人做了它所说的。它阻止了提交给数据库的价值。只读只是阻止编辑,但不会导致表单提交问题。用户类型将遵循相同的诉讼。

#2


0  

I already have my own answer to this Here how I solved this:

我已经有了自己的答案在这里我是如何解决这个问题的:

<select class="form-control" name="aGender" >
     <?php
       if ($userRow['agentGender'] == 'Male') {

           echo "<option selected>Male</option>";
           echo "<option>Female</option>";

       } else {
           echo "<option selected>Female</option>";
           echo "<option>Male</option>";
       }
    ?>
</select>

#3


0  

I think you can use below conditional code for this issue:

我认为你可以使用下面的条件代码来解决这个问题:

<?php if($userRow['aGender'] == 'Male'){
 $selectedMale =  'selected="selected"'
 $selectedFemale =  ''
}else{
  $selectedFemale =  'selected="selected"'
 $selectedMale =  ''
}?>

<select class="form-control" name="aGender" >
           <option selected disabled>*Gender</option>
          <option <?php echo $selectedMale ;?>>Male</option>
          <option <?php echo $selectedFemale;?>>Female</option>

   </select>

and same for the another select box.

和另一个选择框相同。

It will work hopefully.

它会有希望地工作。

#4


0  

In my opinion it's best to use ternary operator in such a case, combined with options element fetched from db/array. Something like:

在我看来,最好在这种情况下使用三元运算符,并结合从db / array中获取的options元素。就像是:

    <?php $options = Array(1 => "Option 1", 2 => "Option 2")
    <select>
    <?php foreach($options as $key => $opt): ?>
        <option value="<?= $key ?>" ($key == $userRow['value'] ? "selected" : "" )><?= $opt ?></option>
    <?php endforeach; ?>
    </select>