国家下拉列表将ID而不是Name(字符串)插入数据库(mysql)

时间:2017-05-24 02:12:01

标签: php jquery mysql ajax post

嗨,所有专家都在那里,我的网站问题是我的下拉列表作为POST方法(对于一个国家,州和城市)工作完全正常,除了一个问题,它将将ID插入我的数据库而不是实际名称。

我感到很困惑,因为它在网页上显示一切正常(一个适当的名字显示在一个国家,州和城市。

请帮我确定问题在我的代码中的位置。

提前多多感谢。

这是我的注册页码

<div>
                  <select name="country" class="country">
                  <option value="<?php echo $row['country_name'] ?>">--Select Country--</option>
                  <?php
                      $stmt = $conn->prepare("SELECT * FROM countries");
                      $stmt->execute();
                      while($row=$stmt->fetch(PDO::FETCH_ASSOC))
                      {
                  ?>
                  <option value="<?php echo $row['country_id']; ?>"><?php echo $row['country_name']; ?></option>
                  <?php
                      } 
                  ?>
                  </select>

                  <select name="state" class="state">
                      <option value="<?php echo $row['state_name'] ?>">--Select State--</option>
                  </select>

                  <select name="city" class="city">
                      <option value="<?php echo $row['city_name'] ?>">--Select City--</option>
                  </select>
                </div>
                <input type="submit" name="registration" value="REGISTER">
                <input type="reset" value=". . . RESET . . .">

以下是从以下字段中获取数据的另一个代码

获取州代码

<?php
include('db_conn.php');
if($_POST['id'])
{
    $id=$_POST['id'];

    $stmt = $conn->prepare("SELECT * FROM states WHERE country_id=:id");
    $stmt->execute(array(':id' => $id));
    ?><option selected="selected">--Select State--</option><?php
    while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
        ?>
            <option value="<?php echo $row['state_id']; ?>"><?php echo $row['state_name']; ?></option>
        <?php
    }
}
?>

获取城市

    <?php
    include('db_conn.php');
    if($_POST['id'])
    {
        $id=$_POST['id'];

        $stmt = $conn->prepare("SELECT * FROM cities WHERE state_id=:id");
        $stmt->execute(array(':id' => $id));
        ?><option selected="selected">--Select City--</option>
        <?php while($row=$stmt->fetch(PDO::FETCH_ASSOC))
        {
            ?>
            <option value="<?php echo $row['city_id']; ?>"><?php echo $row['city_name']; ?></option>
            <?php
        }
    }

?>

和Ajax数据

<?php
//Include database configuration file
include('db_conn.php');

if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
    //Get all state data
    $query = $conn->query("SELECT * FROM states WHERE country_id = ".$_POST['country_id']." AND status = 1 ORDER BY state_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display states list
    if($rowCount > 0){
        echo '<option value="">Select state</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
        }
    }else{
        echo '<option value="">State not available</option>';
    }
}

if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
    //Get all city data
    $query = $conn->query("SELECT * FROM cities WHERE state_id = ".$_POST['state_id']." AND status = 1 ORDER BY city_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display cities list
    if($rowCount > 0){
        echo '<option value="">Select city</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
        }
    }else{
        echo '<option value="">City not available</option>';
    }
}
?>

1 个答案:

答案 0 :(得分:1)

插入选项值。

echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';

在这种情况下是city_id

如果您想要名称,请将其设置为值:

 echo '<option value="'.$row['city_name'].'">'.$row['city_name'].'</option>';