用现有值填充 select 框

Populating select box with existing value

我创建了一个允许用户编辑数据库中现有数据的表单,我将信息从一个页面拉到下一个页面以填充文本框和 select 框。我已经设法用正确的值填充 select 框,但是当更新语句通过它时,它会删除或不识别预先存在的值。有人可以帮忙吗?

if (isset($_POST['submit'])) {
        // Process the form

        if (empty($errors)) {

            $id = $brand["brandId"];
            $brandName = mysql_prep($_POST["brandName"]);
            $brandCategory = mysql_prep($_POST["brandCategory"]);
            $brandKeyword = mysql_prep($_POST["brandKeyword"]);
            $addedBy = mysql_prep($_SESSION['username']);
            $query  = "UPDATE brands SET ";
            $query .= "brandName = '{$brandName}', ";
            $query .= "brandCategory = '{$brandCategory}', ";
            $query .= "brandKeyword = '{$brandKeyword}', ";
            $query .= "addedBy = '{$addedBy}', ";
            $query .= "dateTime = CURRENT_TIMESTAMP ";
            $query .= "WHERE brandId = '{$id}' ";
            $query .= "LIMIT 1";
            $result = mysqli_query($connection, $query);

            if ($result && mysqli_affected_rows($connection) == 1) {
                // Success
                $_SESSION["message"] = "Brand updated.";
                redirect_to("search.php");
            } else {
                // Failure
                $_SESSION["message"] = "Brand update failed.";
            }
        }

    } else {
        // This is probably a GET request

    } // end: if (isset($_POST['submit']))
?>

<?php $layout_context = "user"; ?>
<?php include("../includes/layouts/header.php"); ?>
<?php include("../includes/layouts/navigation.php"); ?>
    <div class="section">
        <div id="message">
            <?php echo message(); ?>
            <?php echo form_errors($errors); ?>
        </div>
            <form id="edit_brands" action="edit_brands.php?id=<?php echo urlencode($brand["brandId"]); ?>" method="post">
                    <h2>Edit Brand Information: <?php echo htmlentities($brand["brandName"]);?></h2>
                    <p>
                        <label for="bname">Brand Name:</label>
                        <input class="textbox" id="bname" type="text" name="brandName" value="<?php echo htmlentities($brand["brandName"]); ?>" autofocus/>
                    </p>
                    <p>
                        <label for="bcategory">Brand Category:</label>
                        <select class="textbox" id="bcategory" type="text" name="brandCategory">
                            <option value=""><?php echo htmlentities($brand["brandCategory"]); ?></option>
                            <option value="Animation">Animation</option>
                            <option value="Automotive">Automotive</option>
                            <option value="Beauty and Fashion">Beauty & Fashion</option>
                            <option value="Comedy">Comedy</option>
                            <option value="Cooking and Health">Cooking & Health</option>
                            <option value="DIY">DIY</option>
                            <option value="Fashion">Fashion</option>
                            <option value="Film and Entertainment">Film & Entertainment</option>
                            <option value="Food and Drink">Food & Drink</option>
                            <option value="Gaming">Gaming</option>
                            <option value="Lifestyle">Lifestyle</option>
                            <option value="Music">Music</option>
                            <option value="News and Politics">News & Politics</option>
                            <option value="Science&Education">Science & Education</option>
                            <option value="Sports">Sports</option>
                            <option value="Technology">Technology</option>
                            <option value="Television">Television</option>
                        </select>
                    </p>
                    <p>
                        <label for="bkeyword">Brand Keyword:</label>
                        <textarea class="FormElement" id="bkeyword" name="brandKeyword" id="brandKeyword" placeholder=""><?php echo htmlentities($brand["brandKeyword"]); ?></textarea>
                    </p>
                    <p>
                    <input type="submit" class="button" name="submit" value="Edit Brand" onclick="return confirm('Do you wish to edit brand?');"/>
                    </p>
                    <p>
                    <a href="search.php" class="link">Cancel</a>
                    </p>
                </form>
        </div>
    </div>

最好的方法是从数组构建 select。

例如:

<?php
$array = array('Animation', 'Automotive', 'Beauty and Fashion ', ...);
echo '<select class="textbox" id="bcategory" type="text" name="brandCategory">';

foreach ($array as $value){
    if($value == htmlentities($brand["brandCategory"]){
        echo '<option value='.$value.' selected>'.$value.'</option>';
    }else{
        echo '<option value='.$value.'>'.$value.'</option>';
    }
}
echo '</select>;

这样你可以检查数组中的值是否与 post 收到的值相同,然后将 selected 属性添加到 option 标签。