使用PHP中的下拉列表更新成员资格表单

时间:2017-08-02 08:39:56

标签: php forms select dropdown

我正在学习PHP,我很难找到解决问题的方法。我创建了一个可以编辑会员资料的页面。我的所有'type = text'字段都正确显示了成员的当前值。但是在2个下拉字段(语言和兴趣)中选择的值不会显示在编辑字段中。当我想编辑成员的“语言”和“兴趣”字段时,它们会更新到MySql,但会显示“选择一个...”选项。

如果需要编辑成员,我应该怎么做才能使存储在数据库中的2个下拉列表的当前值显示在ui上?

这是我的PHP代码:

<?php 

                $id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not Found');

                include('dbconnect.php');

                try{

                    $sql = "SELECT id, firstName, lastName, idNumber, mobileNumber, email, birthDate, languageType, interest FROM members WHERE id = ? LIMIT 0,1";
                    $stmt = $conn->prepare($sql);

                    $stmt->bindParam(1, $id);
                    $stmt->execute();

                    $row = $stmt->fetch(PDO::FETCH_ASSOC);

                    $firstName = $row['firstName'];
                    $lastName = $row['lastName'];
                    $idNumber = $row['idNumber'];
                    $mobileNumber = $row['mobileNumber'];
                    $email = $row['email'];
                    $birthDate = $row['birthDate'];
                    $languageType = $row['languageType'];
                    $interest = $row['interest'];

                }

                catch(PDOException $exception){
                    die('ERROR: '.$exception->getMessage());
                }

            ?>

            <?php

            $id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');

            include 'dbconnect.php';

            if($_POST){

                try{

                    $sql = "UPDATE members SET 
                                            firstName=:firstName, 
                                            lastName=:lastName, 
                                            idNumber=:idNumber,
                                            mobileNumber=:mobileNumber,
                                            email=:email,
                                            birthDate=:birthDate,
                                            languageType=:languageType,
                                            interest=:interest
                                    WHERE   id=:id";

                    $stmt = $conn->prepare($sql);

                    $firstName = htmlspecialchars(strip_tags($_POST['firstName']));
                    $lastName = htmlspecialchars(strip_tags($_POST['lastName']));
                    $idNumber = htmlspecialchars(strip_tags($_POST['idNumber']));
                    $mobileNumber = htmlspecialchars(strip_tags($_POST['mobileNumber']));
                    $email = htmlspecialchars(strip_tags($_POST['email']));
                    $birthDate = htmlspecialchars(strip_tags($_POST['birthDate']));
                    $languageType = $_POST['languageType'];
                    $interest = $_POST['interest'];

                    $stmt->bindParam(':firstName', $firstName);
                    $stmt->bindParam(':lastName', $lastName);
                    $stmt->bindParam(':idNumber', $idNumber);
                    $stmt->bindParam(':mobileNumber', $mobileNumber);
                    $stmt->bindParam(':email', $email);
                    $stmt->bindParam(':birthDate', $birthDate);
                    $stmt->bindParam(':languageType', $languageType);
                    $stmt->bindParam(':interest', $interest);
                    $stmt->bindParam(':id', $id);

                    if($stmt->execute()){
                        echo "<div class='alert alert-success'>Member was updated.</div>";
                    }else{
                        echo "<div class='alert alert-danger'>Unable to update member. Please try again.</div>";
                    }

                }

                catch(PDOException $exception){
                    die('ERROR: ' . $exception->getMessage());
                }
            }
            ?>

这是html:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
                <table class='table table-hover table-responsive table-bordered'>
                    <tr>
                        <td>First Name</td>
                        <td><input type='text' name='firstName' value="<?php echo htmlspecialchars($firstName, ENT_QUOTES);  ?>" class='form-control' /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type='text' name='lastName' value="<?php echo htmlspecialchars($lastName, ENT_QUOTES);  ?>" class='form-control' /></td>
                    </tr>
                    <tr>
                        <td>ID Number</td>
                        <td><input type='text' name='idNumber' value="<?php echo htmlspecialchars($idNumber, ENT_QUOTES);  ?>" class='form-control' /></td>
                    </tr>
                    <tr>
                        <td>Mobile Number</td>
                        <td><input type='text' name='mobileNumber' value="<?php echo htmlspecialchars($mobileNumber, ENT_QUOTES);  ?>" class='form-control' /></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><input type='text' name='email' value="<?php echo htmlspecialchars($email, ENT_QUOTES);  ?>" class='form-control' /></td>
                    </tr>
                    <tr>
                        <td>Birth Date</td>
                        <td><input type='date' name='birthDate' value="<?php echo htmlspecialchars($birthDate, ENT_QUOTES);  ?>" class='form-control' /></td>
                    </tr>
                    <tr>
                        <td>Language</td>
                        <td>

                            <select name='languageType' class='form-control' value="<?php echo $languageType;  ?>" />

                              <option>Select One...</option>
                              <option>Afrikaans</option>
                              <option>English</option>
                              <option>Zulu</option>
                              <option>Xhosa</option>
                              <option>Venda</option>
                              <option>French</option>

                        </td>
                    </tr>
                    <tr>
                        <td>Interest</td>
                        <td>

                            <select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES);  ?>" />

                                  <option>Select One...</option>
                                  <option>Golf</option>
                                  <option>Rugby</option>
                                  <option>Tennis</option>
                                  <option>Cricket</option>
                                  <option>Swimming</option>
                                  <option>Hiking</option>
                                  <option>Surfing</option>
                                  <option>Movies</option>
                                  <option>Swords</option>

                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <input type='submit' value='Save Changes' class='btn btn-primary' />
                            <a href='index.php' class='btn btn-danger'>Back to read members</a>
                        </td>
                    </tr>
                </table>
            </form>

1 个答案:

答案 0 :(得分:0)

这是错的:

<select name='languageType' class='form-control' value="<?php echo $languageType;  ?>" />

                              <option>Select One...</option>
                              <option>Afrikaans</option>
                              <option>English</option>
                              <option>Zulu</option>
                              <option>Xhosa</option>
                              <option>Venda</option>
                              <option>French</option>


  <select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES);  ?>" />

                                  <option>Select One...</option>
                                  <option>Golf</option>
                                  <option>Rugby</option>
                                  <option>Tennis</option>
                                  <option>Cricket</option>
                                  <option>Swimming</option>
                                  <option>Hiking</option>
                                  <option>Surfing</option>
                                  <option>Movies</option>
                                  <option>Swords</option>

Select没有值属性,value属性属于option。

这是您的选择应该如何:

<select name='languageType' class='form-control' />
<option value="Afrikaans">Afrikaans</option>
... <!-- Other options just like I did the first one -->
</select>

如果您希望选择数据库中的值,则需要检查该选项是否不等于db值,然后使用选项的selected属性选择它。

喜欢:

<select name='languageType' class='form-control' />
    <option value="">Select One...</option>
    <option value="Afrikaans"<?php if($languageType == "Afrikaans"){echo "selected='selected'";?>>Afrikaans</option>
    <option value="English" <?php if($languageType == "English"){echo "selected='selected'";?>>English</option>
    <option value="Zulu" <?php if($languageType == "English"){echo "selected='selected'";?>>Zulu</option>
    <option value="Xhosa" <?php if($languageType == "Xhosa"){echo "selected='selected'";?>>Xhosa</option>
    <option value="Venda" <?php if($languageType == "Venda"){echo "selected='selected'";?>>Venda</option>
    <option value="French" <?php if($languageType == "French"){echo "selected='selected'";?>>French</option>
</select>

然后按照上面的第二个下拉菜单作为指南,也不要忘记关闭选择选项</select>