douzhi8244 2014-09-24 19:33
浏览 22

国家不回复表格?

I asked this is another forum a few weeks ago and no one was able to answer it, hoping someone here could. The state chosen goes to the database, but when the page is refreshed, it only shows select and not the state. Same goes with the the gender but gender doesn't even post in the database.

<?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条回答 默认 最新

  • dongyao5186 2014-09-24 19:42
    关注

    You need to use selected property of appropriate corresponding option.

    Typically this is done programmatically by doing something like this:

    //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>';
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败