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

时间:2013-09-11 05:49:04

标签: php mysql

我需要从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 
  }
?>

15 个答案:

答案 0 :(得分:23)

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

<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>

答案 1 :(得分:6)

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

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

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

答案 2 :(得分: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>

答案 3 :(得分:1)

最佳代码和简单

<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>

答案 4 :(得分:1)

最简便的解决方案

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

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

答案 5 :(得分: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>
// 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 
}
?>

答案 6 :(得分: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>

答案 7 :(得分:0)

从下拉列表中选择值。

  <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> 

答案 8 :(得分:0)

使用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>

答案 9 :(得分:0)

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

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

答案 10 :(得分:0)

答案很简单。 当您通过下拉菜单传递值时。

就像其他一样使用。

例如:

foreach($result as $row) {                      
   $GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'. 
   ($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').' 
   >'.$row['valueetc'].'</option>';
}

答案 11 :(得分: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 
  }
?>

答案 12 :(得分: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>

答案 13 :(得分:0)

$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>

答案 14 :(得分:-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. 然后它会更新你的记录,在这里你不必一一创建列表