国家不回复表格?

时间:2014-09-24 19:33:50

标签: php mysql mysqli phpmyadmin

我问这是几个星期前的另一个论坛,没有人能够回答它,希望有人在这里。选择的状态将转到数据库,但刷新页面时,它只显示选择而不显示状态。性别也是如此,但性别甚至没有在数据库中发布。

<?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>

1 个答案:

答案 0 :(得分:0)

您需要使用相应选项的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>';
    }
}
?>