几周前我问这是另一个论坛,没人能回答,希望有人可以。所选状态进入数据库,但是刷新页面时,仅显示选择状态,而不显示状态。性别也一样,但性别甚至不发布在数据库中。

<?php

    if( !isset( $_SESSION ) ){

        session_start();

    }


    $con=mysqli_connect("localhost", "root", "", "test");

    // Check connection

    if (mysqli_connect_errno()) {

      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    }

    // escape variables for security

    $firstname = mysqli_real_escape_string($con, $_POST['firstname']);
    $lastname = mysqli_real_escape_string($con, $_POST['lastname']);
    $gender = mysqli_real_escape_string($con, $_POST['gender']);
    $number = mysqli_real_escape_string($con, $_POST['number']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $addressone = mysqli_real_escape_string($con, $_POST['addressone']);
    $addresstwo = mysqli_real_escape_string($con, $_POST['addresstwo']);
    $city = mysqli_real_escape_string($con, $_POST['city']);
    $state = mysqli_real_escape_string($con, $_POST['state']);
    $zip = mysqli_real_escape_string($con, $_POST['zip']);


     $sql="UPDATE users SET firstname='$firstname', lastname = '$lastname', gender = '$gender,'number='$number', email = '$email',addressone='$addressone', addresstwo= '$addresstwo',`city`='$city', state = '$state', zip = '$zip' WHERE id='" .$_SESSION['id']."'";



    if (!mysqli_query($con,$sql)) {

      die('Error: ' . mysqli_error($con));
    }
    header("Location:dashboard.php");

    mysqli_close($con);

?>




Part of the entire form

<div class="form-group">
            <label class="col-md-5 control-label" name="state">State</label>
                <div class="col-md-2">
                    <select id="state" name="state" class="form-control" value="<?php echo $state; ?>">
                        <option>Select</option>
                        <option>Alabama</option>
                        <option>Alaska</option>
                        <option>Arizona</option>
                        <option>Arkansas</option>
                        <option>California</option>
                        <option>Colorado</option>
                        <option>Connecticut</option>
                        <option>Delaware</option>
                        <option>District of Colombia</option>
                        <option>Florida</option>
                        <option>Georgia</option>
                        <option>Hawaii</option>
                        <option>Idaho</option>
                        <option>Illinois</option>
                        <option>Indiana</option>
                        <option>Iowa</option>
                        <option>Kansas</option>
                        <option>Kentucky</option>
                        <option>Louisiana</option>
                        <option>Maine</option>
                        <option>Maryland</option>
                        <option>Massachusetts</option>
                        <option>Michigan</option>
                        <option>Minnesota</option>
                        <option>Mississippi</option>
                        <option>Missouri</option>
                        <option>Montana</option>
                        <option>Nebraska</option>
                        <option>Nevada</option>
                        <option>New Hampshire</option>
                        <option>New Jersey</option>
                        <option>New Mexico</option>
                        <option>New York</option>
                        <option>North Carolina</option>
                        <option>North Dakota</option>
                        <option>Ohio</option>
                        <option>Oklahoma</option>
                        <option>Oregon</option>
                        <option>Pennsylvania</option>
                        <option>Rhode Island</option>
                        <option>South Carolina</option>
                        <option>South Dakota</option>
                        <option>Tennessee</option>
                        <option>Texas</option>
                        <option>Utah</option>
                        <option>Vermont</option>
                        <option>Washington</option>
                        <option>West Virginia</option>
                        <option>Wisconsin</option>
                        <option>Wyoming</option>
                        <option>American Samoa</option>
                        <option>Federated States of Micronesia</option>
                        <option>Guam</option>
                        <option>Marshall Islands</option>
                        <option>Northern Mariana Islands</option>
                        <option>Puerto Rico</option>
                        <option>Virgin Islands</option>
                        <option>Palau</option>
                        <option>AA</option>
                        <option>AE</option>
                        <option>AP</option>
                    </select>
                </div>
        </div>



Gender portion of form


    <!-- Multiple Radios (inline) -->
        <div class="form-group">
            <label class="col-md-5 control-label" for="gender">Gender</label>
                <div class="col-md-2">
                    <label class="radio-inline" for="gender-0">
                        <input type="radio" name="gender" id="gender-0"checked="checked" value="<?php echo $gender; ?>">
                            Male
                    </label>
                    <label class="radio-inline" for="gender-1">
                        <input type="radio" name="gender" id="gender-1" value="<?php echo $gender; ?>">
                            Female
                    </label>
            </div>
        </div>

最佳答案

您需要使用适当的相应选项的selected属性。

通常,这是通过编程执行以下操作:

//define array of all available states somehwere before you get to outputting the select
$state_options = array(
    'Alaska',
    'Alabama',
    ...
);

// then when outputting your state option elements, do it in a loop like this
<option value="">Select</option>
<?php
foreach($state_options as $state_option) {
    if ($state_option === $state) {
        // this is selected state
        echo '<option value="' . $state_option . ' selected="selected">' . $state_option . '</option>';
    } else {
        // this is non selected option
        echo '<option value="' . $state_option . '>' . $state_option . '</option>';
    }
}
?>

关于php - 国家不发回表格吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26025019/

10-12 13:04