php 使用php mysql错误从数据库中选择的值进入下拉选择框选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18733545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:06:45  来源:igfitidea点击:

selected value get from db into dropdown select box option using php mysql error

phpmysql

提问by Php Gemini

I need to get selected value from db into select box. please, tell me how to do it. Here is the code. Note: 'options' value depends on the category.

我需要从 db 中获取选定的值到选择框中。请告诉我怎么做。这是代码。注意:'options' 值取决于类别。

<?php 
  $sql = "select * from mine where username = '$user' ";
  $res = mysql_query($sql);
  while($list = mysql_fetch_assoc($res)){
    $category = $list['category'];
    $username = $list['username'];
    $options = $list['options'];
?>

<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
   <option value="0">Please Select Option</option>
   <option value="PHP">PHP</option>
   <option value="ASP">ASP</option>
</select>

<?php 
  }
?>

回答by rakeshjain

I think you are looking for below code changes:

我认为您正在寻找以下代码更改:

<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
<option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option>
</select>

回答by independent.guru

The easiest way I can think of is the following:

我能想到的最简单的方法如下:

PHP

PHP

<?php
$selection=array('PHP','ASP');
echo '<select>
      <option value="0">Please Select Option</option>';

foreach($selection as $selection){
    $selected=($options == $selection)? "selected" : "";
echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>';
    }

echo '</select>';

The code basically places all of your options in an array which are called upon in the foreach loop. The loop checks to see if your $options variable matches the current selection it's on, if it's a match then $selected will = selected, if not then it is set as blank. Finally the option tag is returned containing the selection from the array and if that particular selection is equal to your $options variable, it's set as the selected option.

代码基本上将所有选项放在一个数组中,这些选项在 foreach 循环中被调用。循环检查您的 $options 变量是否与它所在的当前选择匹配,如果匹配,则 $selected 将 = 选择,如果不匹配,则将其设置为空白。最后返回包含数组中选择的选项标签,如果该特定选择等于您的 $options 变量,则将其设置为所选选项。

回答by chirag ode

for example ..and please use mysqli() next time because mysql() is deprecated.

例如..下次请使用 mysqli() 因为 mysql() 已被弃用。

<?php
$select="select * from tbl_assign where id='".$_GET['uid']."'"; 
$q=mysql_query($select) or die($select);
$row=mysql_fetch_array($q);
?>

<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php $s="select * from tbl_new_user where type='client'";
$q=mysql_query($s) or die($s);
while($rw=mysql_fetch_array($q))
{ ?>
<option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option>
<?php } ?>
</select>

回答by Siddharth Shukla

Select value from drop down.

从下拉列表中选择值。

  <select class="form-control" name="category" id="sel1">
                   <?php         
                    foreach($data as $key =>$value){ 
                      ?>                         
                           <option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option>
                    <?php } ?>
   </select> 

回答by razvan

BEST code and simple

最好的代码和简单

<select id="example-getting-started" multiple="multiple" name="category">

    <?php
    $query = "select * from mine";
    $results = mysql_query($query);

    while ($rows = mysql_fetch_assoc(@$results)){ 
    ?>
    <option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option>

    <?php
    } 
    ?>
</select>

回答by K. Raza

THE EASIEST SOLUTION

最简单的解决方案

It will add an extra in your options but your problem will be solved.

它会在您的选项中添加额外的内容,但您的问题将得到解决。

<?php 
    if ($editing == Yes) {
        echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
    }
?>

回答by narinder kumar

You can also do like this ....

你也可以这样做......

<?php  $countryname = $all_meta_for_user['country']; ?>

<select id="mycountry"  name="country" class="user">

    <?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" );
    foreach($myrows as $rows){
        if( $countryname == $rows->id ){ 
            echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>";
        } else{ 
            echo "<option value='".$rows->id."'>".$rows->country_name."</option>";
        }
    }
    ?>
</select>

回答by 7ani9

USING POD

使用 POD

<?php
$username = "root";
$password = "";
$db = "db_name";

$dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
    $conn = new PDO($dns,$username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "select * from mine where username = ? ";

    $stmt1 = $conn->prepare($sql);
    $stmt1 -> execute(array($_POST['user']));
    $all = $stmt1->fetchAll(); ?>

    <div class="controls">
        <select  data-rel="chosen"  name="degree_id" id="selectError">

                 <?php foreach($all as $nt) { echo "<option value =$nt[id]>$nt[name]</option>";}?>
        </select>

    </div>

回答by Nasser

Just Add an extra hidden option and print selected value from database

只需添加一个额外的隐藏选项并从数据库中打印选定的值

<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>

回答by Pradeeshnarayan

This may help you.

这可能对你有帮助。

?php 
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res))
{
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
// Assuming $list['options'] is a coma seperated options string 
$arr=explode(",",$list['options']);
<?php foreach ($arr as $value) { ?>
   <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } >
</select>

<?php 
}
?>