简体   繁体   中英

How to insert dropdown list option into database

<tr>
    <td>Crew Name:</td>
    <td><input type="text" name="CrewName" id ="CrewName"required></td>     
</tr>
<tr>
    <td>Crew Rank:</td>
    <td>
        <input type="hidden" name="CrewRank" id="CrewRank" required>
        <select>
            <?php
            $con = getDbConnect();
            if (!mysqli_connect_errno($con)) {
                $queryStr = "SELECT rank "."FROM rankpage";
            }

            $result = mysqli_query($con, $queryStr);
            while ($row = mysqli_fetch_array($result)) {
                echo "<option>" . $row['rank'] . "</option>";
            }
            ?>
        </select>
        </input>
    </td>       
</tr>

When I tried to use a dropdown list option for my input form, I could get the dropdown to show but the selected result doesn't go into the database. I'm not sure how to link my select option as an input type or the id to my selected result

Check the value attribute of the option tag. You need to set the value to POST.

It should be something like:

echo "<option value=\"" . $row['rank'] . "\">" . $row['rank'] . "</option>";

And pass a name attribute (eg rank ) to select tag. You should get the value in $_REQUEST['rank'] . You can you use $_GET or $_POST , depending on method passed in <form>

You do not need link select option as input type. All you need is add name to the select element and add value for option element as:

<tr>
    <td>Crew Name:</td>
    <td>
        <input type="text" name="CrewName" id ="CrewName" required />
    </td>     
</tr>
<tr>
    <td>Crew Rank:</td>
    <td>
        <select name="CrewRank" id="CrewRank">
        <?php
             $con = getDbConnect();
             if (!mysqli_connect_errno($con)) {
                 $queryStr = "SELECT rank " .
                             "FROM rankpage";

             }

             $result = mysqli_query($con, $queryStr);

             while ($row = mysqli_fetch_array($result)) {
                    echo "<option value=\"" . $row['rank'] . "\">" . $row['rank'] . "</option>";
             }
         ?>
         </select>
     </td>       
</tr>

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