dtlc84438 2019-07-24 15:43
浏览 100

如何使我的编辑php自动选择数据库中的下拉菜单选项

I can't get my dropdown menu (in edit.php) to select the option that i choose. I can also not edit the value through text form, it just gets submited but nothing happends (I can edit other values with it)

Ive already tried with no luck

<?php
    if($row["rank"]=='Unranked')
    {
        echo "Unranked";
    }
?>

This is my option code

        <select id="rank" name="rank">
               <option value = "No Selected">Select a rank</option>
               <option value = "Unranked">Unranked</option>
               <option value = "Silver 1">Silver 1</option>
               <option value = "Silver 2">Silver 2</option>
               <option value = "Silver 3">Silver 3</option>
               <option value = "Silver 4">Silver 4</option>
               <option value = "Silver Elite">Silver Elite</option>
               <option value = "Silver Elite Master">Silver Elite Master</option>
               <option value = "Gold Nova 1">Gold Nova 1</option>
               <option value = "Gold Nova 2">Gold Nova 2</option>
               <option value = "Gold Nova 3">Gold Nova 3</option>
               <option value = "Gold Nova master">Gold Nova master</option>
               <option value = "Master Guardian 1">Master Guardian 1</option>
               <option value = "Master Guardian 2">Master Guardian 2</option>
               <option value = "Master Guardian Elite">Master Guardian Elite</option>
               <option value = "Distinguished Master Guardian">Distinguished Master Guardian</option>
               <option value = "Legendary Eagle">Legendary Eagle</option>
               <option value = "Legendary Eagle Master">Legendary Eagle Master</option>
               <option value = "Supreme">Supreme Master First Class</option>
               <option value = "Global Elite">Global Elite</option>
        </select>


I would like it to auto select the selected option and to acctually work

EDIT 54523(im new to this site sigh):

<?php
// including the database connection file
include_once("config.php");

if(isset($_POST['update']))
{    
    $Id = $_POST['Id'];

    $username=$_POST['username'];
    $password=$_POST['password'];
    $friendcode=$_POST['friendcode']; 
    $rank = $_POST['rank'];
    $lvl = $_POST['lvl'];   

    // checking empty fields
    if(empty($username) || empty($password) || empty($friendcode) || empty($rank) || empty($lvl)) {            
        if(empty($username)) {
            echo "<font color='red'>Please enter a username</font><br/>";
        }

        if(empty($password)) {
            echo "<font color='red'>Please enter a password</font><br/>";
        }

        if(empty($friendcode)) {
            echo "<font color='red'>Please enter a friendcode</font><br/>";
        }        
        if(empty($rank)) {
            echo "<font color='red'>Please select a rank</font><br/>";
        } 
        if(empty($lvl)) {
            echo "<font color='red'>Please select a level</font><br/>";
        } 
    } else {    
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE legit SET username='$username',password='$password',friendcode='$friendcode' WHERE Id=$Id");

        //redirectig to the display page. In our case, it is index.php
        header("Location: index.php");
    }
}
?>
<?php
//getting id from url
$Id = $_GET['Id'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM legit WHERE Id=$Id");

while($res = mysqli_fetch_array($result))
{
    $username= $res['username'];
    $password= $res['password'];
    $friendcode= $res['friendcode'];
    $rank= $res['rank'];
    $lvl= $res['lvl'];
}
?>
<html>
<head>    
    <title>Edit account ID <?php echo $Id;?></title>
</head>

<body>
    <div align="center">
    <a href="index.php">Go back to account list</a>
    <br/><br/>

    <form name="form1" method="post" action="edit.php">
        <table border="0">
            <tr> 
                <td>Username</td>
                <td><input type="text" name="username" value="<?php echo $username;?>"></td>
            </tr>
            <tr> 
                <td>Password</td>
                <td><input type="text" name="password" value="<?php echo $password;?>"></td>
            </tr>
            <tr> 
                <td>Friendcode</td>
                <td><input type="text" name="friendcode" value="<?php echo $friendcode;?>"></td>
            </tr>
            <tr>
        <td>Rank</td>
        <td>
        <select id="rank" name="rank">
               <option value = "No Selected">Select a rank</option>
               <option value = "Unranked">Unranked</option>
               <option value = "Silver 1">Silver 1</option>
               <option value = "Silver 2">Silver 2</option>
               <option value = "Silver 3">Silver 3</option>
               <option value = "Silver 4">Silver 4</option>
               <option value = "Silver Elite">Silver Elite</option>
               <option value = "Silver Elite Master">Silver Elite Master</option>
               <option value = "Gold Nova 1">Gold Nova 1</option>
               <option value = "Gold Nova 2">Gold Nova 2</option>
               <option value = "Gold Nova 3">Gold Nova 3</option>
               <option value = "Gold Nova master">Gold Nova master</option>
               <option value = "Master Guardian 1">Master Guardian 1</option>
               <option value = "Master Guardian 2">Master Guardian 2</option>
               <option value = "Master Guardian Elite">Master Guardian Elite</option>
               <option value = "Distinguished Master Guardian">Distinguished Master Guardian</option>
               <option value = "Legendary Eagle">Legendary Eagle</option>
               <option value = "Legendary Eagle Master">Legendary Eagle Master</option>
               <option value = "Supreme">Supreme Master First Class</option>
               <option value = "Global Elite">Global Elite</option>
        </select>

        </td>
        </tr> 

            </tr>

            <tr>
                <td><input type="hidden" name="Id" value=<?php echo $_GET['Id'];?>></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
    </div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongsou0083 2019-07-24 23:21
    关注

    The easiest way I can think of to do this would be to store your rankings in the database. Then, when the page loads, execute a SELECT query to pull the ranking options from the database. Next, loop through those results to build your drop down list checking to see if the current value matches the value selected. If it does, add selected to the <option></option> tag. For example:

    <?php
    
    // including the database connection file
    include_once("config.php");
    
    if(isset($_POST['update'])) {
        $Id = $_POST['Id'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $friendcode = $_POST['friendcode'];
        $rank = $_POST['rank'];
        $lvl = $_POST['lvl'];
    
        // checking empty fields
        if (empty($username) || empty($password) || empty($friendcode) || empty($rank) || empty($lvl)) {
            if (empty($username)) {
                echo "<font color='red'>Please enter a username</font><br/>";
            }
    
            if (empty($password)) {
                echo "<font color='red'>Please enter a password</font><br/>";
            }
    
            if (empty($friendcode)) {
                echo "<font color='red'>Please enter a friendcode</font><br/>";
            }
    
            if (empty($rank)) {
                echo "<font color='red'>Please select a rank</font><br/>";
            }
    
            if (empty($lvl)) {
                echo "<font color='red'>Please select a level</font><br/>";
            }
        } else {
            // updating the table
            $result = mysqli_query($mysqli, "UPDATE legit SET username='$username',password='$password',friendcode='$friendcode' WHERE Id = $Id");
    
            //redirectig to the display page. In our case, it is index.php
            header("Location: index.php");
        }
    }
    
    // getting id from url
    $Id = $_GET['Id'];
    
    // selecting data associated with this particular id
    $result = mysqli_query($mysqli, "SELECT * FROM legit WHERE Id = $Id");
    
    while($res = mysqli_fetch_array($result)) {
        $username = $res['username'];
        $password = $res['password'];
        $friendcode = $res['friendcode'];
        $rank = $res['rank'];
        $lvl = $res['lvl'];
    }
    
    ?>
    
    <html>
    <head>
        <title>Edit account ID <?php echo $Id;?></title>
    </head>
    <body>
    
    <div align="center">
        <a href="index.php">Go back to account list</a>
    
        <br/><br/>
    
        <form name="form1" method="post" action="edit.php">
            <table border="0">
                <tr>
                    <td>Username</td>
                    <td><input type="text" name="username" value="<?php echo $username;?>"></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="text" name="password" value="<?php echo $password;?>"></td>
                </tr>
                <tr>
                    <td>Friendcode</td>
                    <td><input type="text" name="friendcode" value="<?php echo $friendcode;?>"></td>
                </tr>
                <tr>
                    <td>Rank</td>
                    <td>
                        <select id="rank" name="rank">
                        <?php
    
                            // select ranking options from database
                            $rankings = mysqli_query($mysqli, "SELECT * FROM rankings");
    
                            while($ranking = mysqli_fetch_array($rankings)) {
                                if ($ranking['id'] == $rank) {
                                    echo "<option value=" . $ranking['id'] . " selected>" . $ranking['name'] . "</option>";
                                } else {
                                    echo "<option value=" . $ranking['id'] . ">" . $ranking['name'] . "</option>";
                                }
                            }
                        ?>
                        </select>
                    </td>
                </tr>
                </tr>
                <tr>
                    <td><input type="hidden" name="Id" value=<?php echo $_GET['Id'];?>></td>
                    <td><input type="submit" name="update" value="Update"></td>
                </tr>
            </table>
        </form>
    </div>
    
    </body>
    </html>
    

    When adding selected, the four line IF statement could be swapped out for an inline IF statement, reducing the code. Also, when adding new rankings, you would just add them to the database table instead of having to modify your code. You want as much dynamically driven data as possible, if my opinion.

    Also, stay consistent with your variables. Do not use cAmElCaSe mixed with all lowercase. Consistent see big when it comes to development and having clean code. That's the one big thing I stress to all of my developers.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能