简体   繁体   中英

How do i make my edit php auto select the dropdown menu option that is in the database

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM