PHP的显示输出基于从MySQL下拉

PHP的显示输出基于从MySQL下拉

本文介绍了PHP的显示输出基于从MySQL下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经存在,但是我们在那篇文章中没有得到答案.我一直遇到以下问题.我能够从数据库中检索数据并将其显示在下拉菜单中.但是我不知道如何使用与输入相同的下拉列表并将其嵌入到form.My代码中如下所示.这里从数据库中检索项目名称,并以下拉列表的形式显示.现在,我需要使用相同的下拉列表(HTML表单),该表单将用于将数据输入到另一个表中.下面是我的代码

I know this is already have but we are not getting answer in that post.i have been having the following problem .I am able to retrieve the data from database and display it in a drop down menu.But i dont know how to use the same drop down list as the input and embed it in a form.My code is as follows.Here project names are retrieved from the database and are displayed in the form of drop down list.Now i need to use the same drop down list in the form(HTML form) which will be used to input data into another table. below is my code

 <div id="footer"><?php
    //Include database configuration file
    include('dbConfig.php');

    //Get all state data
    $query = $db->query("SELECT * FROM state WHERE status = 1 ORDER BY state_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;
    ?>
    <select name="state" id="state">
        <option value="">Select state</option>
        <?php
        if($rowCount > 0){
            while($row = $query->fetch_assoc()){
                echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
            }
        }else{
            echo '<option value="">state not available</option>';
        }
        ?>
    </select></div>

if($rowCount > 0){
        echo '<option value="">Select district</option>';
        while($row = $query->fetch_assoc()){
            echo '<option value="'.$row['district_id'].'">'.$row['district_name'].'</option>';
        }
    }else{
        echo '<option value="">district not available</option>';
    }

推荐答案

您需要将Select标记带到if语句的外面,在分支结束时,您需要关闭select标记.而if-else取决于您检索的值.

You need to bring your Select tag out side of the if statement like and at the ending of branching you need to close your select tag. And if-else depends on your retrieved value.

echo '<select name="test">';
    if($rowCount > 0){
        echo '<option value="">Select district</option>';
        while($row = $query->fetch_assoc()){
            echo '<option value="'.$row['district_id'].'">'.$row['district_name'].'</option>';
        }
    }else{
        echo '<option value="">district not available</option>';
    }
    echo '</select>';

这篇关于PHP的显示输出基于从MySQL下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:34