15

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

<?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 
  }
?>
4

16 回答 16

39

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

<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>
于 2013-09-11T06:02:33.107 回答
9

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

<?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>';

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

于 2016-08-22T00:38:56.810 回答
5

例如..请下次使用 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>
于 2013-09-11T06:00:56.830 回答
2

从下拉列表中选择值。

<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> 
于 2017-06-11T14:55:44.090 回答
1

最简单的解决方案

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

<?php 
    if ($editing == Yes) {
        echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
    }
?>
于 2020-05-13T18:08:19.997 回答
1

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

<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
于 2019-08-07T02:58:27.453 回答
1
$option = $result['semester'];
<option >Select</option>
                    <option value="1st" <?php if($option == "1st") echo 'selected = "selected"'; ?>>1st</option>
                    <option value="2nd" <?php if($option == "2nd") echo 'selected = "selected"'; ?>>2nd</option>
                    <option value="3rd" <?php if($option == "3rd") echo 'selected = "selected"'; ?>>3rd</option>
                    <option value="4th" <?php if($option == "4th") echo 'selected = "selected"'; ?>>4th</option>
                    <option value="5th" <?php if($option == "5th") echo 'selected = "selected"'; ?>>5th</option>
                    <option value="6th" <?php if($option == "6th") echo 'selected = "selected"'; ?>>6th</option>
                    <option value="7th" <?php if($option == "7th") echo 'selected = "selected"'; ?>>7th</option>
                    <option value="8th" <?php if($option == "8th") echo 'selected = "selected"'; ?>>8th</option>
                </select>
于 2021-06-12T10:30:26.747 回答
0

答案很简单。当你从下拉列表中传递值时。

就像其他一样使用。

例如:

foreach($result as $row) {                      
   $GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'. 
   ($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').' 
   >'.$row['valueetc'].'</option>';
}
于 2020-08-23T13:58:48.727 回答
0

使用 PDO

<?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>
于 2018-05-20T08:37:58.920 回答
0

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

<?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>
于 2016-05-04T11:12:09.630 回答
0

将 db 中的值放入变量中并像下面的代码示例一样检查

<select class="form-control" name="currency_selling" required >
     <option value="">Select Currency</option>
     <option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
  <option value="dollar"  <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
  <option value="pounds"  <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
  <option value="dirham"  <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
  </select>
于 2021-10-08T07:55:12.667 回答
0
<?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 echo $options == 'PHP' ? 'selected' : ''; ?> >PHP</option>
<option value="ASP" <?php echo $options == 'ASP' ? 'selected' : ''; ?> >ASP</option>
</select>

<?php 
  }
?>
于 2021-03-22T08:49:46.430 回答
0

我正在使用这样的eval() PHP 函数:

我的PHP代码:

$selOps1 = $selOps2 = $selOps3 = '';
eval('$selOps'. $dbRow["DBitem"] . ' = "selected";');

然后在我的选择框中,我像这样使用它:

<select>
   <option <?=$selOps1?> value="1">big</option>
   <option <?=$selOps2?> value="2">Middle</option>
   <option <?=$selOps3?> value="3">Small</option>
</select>
于 2021-05-19T09:17:59.630 回答
0

最好的代码和简单

<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>
于 2015-08-22T13:42:04.180 回答
-1

这可能会对您有所帮助。

?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 
}
?>
于 2013-09-11T06:04:05.530 回答
-1

好的,首先我认为,您需要从数据库中获取一些数据并将其作为选项显示在选择框中,然后您需要将该选择选项值保存到数据库中。

  1. 首先你必须从数据库中获取值
function showAllData(){
    $connection = mysqli_connect('localhost', 'root', '', 'beva');
    $query = "SELECT * FROM users";
    $result = mysqli_query($connection, $query);

    if(!$result){
        die('Query Failed'. mysqli_error());
    }
    while($row = mysqli_fetch_assoc($result)){
        $id = $row['id'];
        echo $id;
        echo "<option name='$id'>$id</option>";
    } 
}
  1. 那么你必须在选择选项上调用这个函数
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</head>
<body>
<div class="container mt-3 mb-5">
    <div class="card">
        <form action="html.php" method="post">
            <input type="text" name="username" placeholder="Enter Username">
            <input type="password" name="password" placeholder="Enter Password">
            <select name="se"> <?php showAllData();?> </select>
            <input type="submit" name="submit">
        </form>
    </div>
</div>
</body>
</html>

在这里,您只需调用该函数。确保您已<select name="se">为选择框命名

  1. 那么您要做的就是将该值保存在数据库中
if(isset($_POST['submit'])){
        $connection = mysqli_connect('localhost', 'root', '', 'beva');
        $username = $_POST['username'];
        $password = $_POST['password'];
        $id = $_POST['se'];
        echo $id;

        $query = "UPDATE users SET username = '$username', password = '$password' WHERE id = $id ";

        $result = mysqli_query($connection, $query);
        echo $query;
        if(!$result){
            echo $query;
            die("Query Failed" . mysqli_error($connection));
        }  
}
  1. 然后它会更新你的记录,在这里你不必一一创建列表
于 2021-03-30T07:19:19.783 回答