使用php将数据显示在数据库的下拉列表中

时间:2015-02-23 09:02:51

标签: php drop-down-menu

这里我试图从数据库中获取数据并使用PHP将其显示在下拉列表中。虽然表格中有数据,但它不会显示在下拉列表中。这背后的问题是什么,解决方案是什么?

    <!DOCTYPE html>

<html>
    <?php
        include 'header.php';
       include 'footer.php';
        include './config.php';
    ?>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
       <div id="page-wrapper">
           <div class="row">
               <div class="col-lg-12">
                   <h1 class="page-header">Add Product</h1>
              </div>
              <!-- /.col-lg-12 -->
          </div>
          <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Product
                    </div>
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <form action="AddProduct.php" method="POST" role="form">
                                    <div class="col-lg-6">
                                    <div class="form-group">
                                        <label>Project Name</label>
                                        <?php 
                                            $sqll="SELECT * FROM project_master";
                                            echo "<select class='form-control' name='project'>";
                                            echo "<option value=''>Select One</option>";  
                                            foreach ($conn->query($sqll) as $row){
                                            echo "<option value=$row[Project_id]>$row[Project_title]</option>";  
                                            }
                                            echo "</select>";

                                        ?>
                                    </div>
                                    <div class="form-group">
                                        <label>Product Name</label>
                                        <input class="form-control" name="Product_name" type="text" id="Product_name" required="required" title="Enter Product Name">

                                    </div>
                                    <button type="submit" class="btn btn-primary" id="submit" data-toggle="modal" data-target="#myModal">Submit Query</button>
                                    <button type="reset" class="btn btn-primary">Reset</button>
                                    </div>

                                    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title" id="myModalLabel">Particular Circle Will be included for selected Project</h4>
                                    </div>
                                    <div class="modal-body">
                                        Do you want to proceed??
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary" id="submit">Save changes</button>
                                    </div>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>

                            </form>
                        </div>
                    </div>

                        </div>
                    </div>

                    </div>   
                </div> 


</body>

页面来源:                   

<head>

    <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Test Management Tool</title>

    <!-- Bootstrap Core CSS -->

    <link href="../../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- MetisMenu CSS -->
    <link href="../../bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="../../dist/css/sb-admin-2.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="../../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-default navbar-static-top"  role="navigation" style="margin-bottom: 0">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="DeshbordAdmin.php"><i class="fa fa-home"></i>Administrator</a>
            </div>
            <!-- /.navbar-header -->

            <ul class="nav navbar-top-links navbar-right">
                <li class="dropdown">
                </li>
                <!-- /.dropdown -->
                <li class="dropdown">

                </li>
                <!-- /.dropdown -->
                <li class="dropdown">

                    <!-- /.dropdown-alerts -->
                </li>
                <!-- /.dropdown -->
                <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                        <i class="fa fa-user fa-fw"></i>  <i class="fa fa-caret-down"></i>
                    </a>
                    <ul class="dropdown-menu dropdown-user">
                        <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
                        </li>
                        <li><a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a>
                        </li>
                        <li class="divider"></li>
                        <li><a href="../../logout.php"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
                        </li>
                    </ul>
                    <!-- /.dropdown-user -->
                </li>
                <!-- /.dropdown -->
            </ul>  
        </nav>
    </div>
    <script src="../../bower_components/jquery/dist/jquery.min.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="../../bower_components/bootstrap/dist/js/bootstrap.min.js">    </script>

    <!-- Metis Menu Plugin JavaScript -->
    <script src="../../bower_components/metisMenu/dist/metisMenu.min.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="../../dist/js/sb-admin-2.js"></script>

    <!-- Page-Level Demo Scripts - Notifications - Use for reference -->
    <script>
    // tooltip demo
    $('.tooltip-demo').tooltip({
        selector: "[data-toggle=tooltip]",
        container: "body"
    })

    // popover demo
    $("[data-toggle=popover]")
        .popover()
    </script>
    </body>
</html><!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Test Management Tool</title>

<!-- Bootstrap Core CSS -->

<link href="../../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href="../../bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="../../dist/css/sb-admin-2.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="../../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

</head>
<body>

<div id="wrapper">

    <!-- Navigation -->

        <div class="navbar-default sidebar" role="navigation">
            <div class="sidebar-nav navbar-collapse">
                <ul class="nav" id="side-menu">
                    <li class="sidebar-search">
                        <div class="input-group custom-search-form">
                            <input type="text" class="form-control" placeholder="Search...">
                            <span class="input-group-btn">
                            <button class="btn btn-default" type="button">
                                <i class="fa fa-search"></i>
                            </button>
                        </span>
                        </div>
                        <!-- /input-group -->
                    </li>
                    <li>
                        <a href="DeshbordAdmin.php"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-users"></i> User<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="AddUser.php">Add User</a>
                            </li>
                            <li>
                                <a href="morris.html">Delete User</a>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-cubes"></i> Project<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="AddProject.php">Add Project</a>
                            </li>
                            <li>
                                <a href="morris.html">View Project</a>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i> Product<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="AddProduct.php">Add Product</a>
                            </li>
                            <li>
                                <a href="morris.html">Assign Product</a>
                            </li>
                            <li>
                                <a href="morris.html">View Product</a>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sitemap fa-fw"></i>Test Suites<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="#">Add Test Suite</a>
                            </li>
                            <li>
                                <a href="#">Delete Test Suite</a>
                            </li>
                            <li>
                                <a href="#">Update Test Suite <span class="fa arrow"></span></a>
                                <ul class="nav nav-third-level">
                                    <li>
                                        <a href="#">Add Test case</a>
                                    </li>
                                    <li>
                                        <a href="#">Delete Test case</a>
                                    </li>
                                </ul>
                                <!-- /.nav-third-level -->
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-wrench fa-fw"></i> Test Case<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="panels-wells.html">Add Test Case</a>
                            </li>
                            <li>
                                <a href="buttons.html">Update Test Case</a>
                            </li>
                            <li>
                                <a href="notifications.html">View Test Case</a>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </li>
                    <li>
                        <a href="tables.html"><i class="fa fa-circle-o-notch"></i> Circe</a>
                    </li>
                    <li>
                        <a href="forms.html"><i class="fa fa-edit fa-fw"></i> Forms</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-files-o fa-fw"></i> Reports<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="blank.html">Blank Page</a>
                            </li>
                            <li>
                                <a href="login.html">Login Page</a>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </li>
                </ul>
            </div>
            <!-- /.sidebar-collapse -->
        </div>
        <!-- /.navbar-static-side -->
    </nav>
</div>
<!-- /#wrapper -->
</body>
</html>
 <head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
   <div id="page-wrapper">
       <div class="row">
           <div class="col-lg-12">
               <h1 class="page-header">Add Product</h1>
          </div>
          <!-- /.col-lg-12 -->
      </div>
      <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Product
                </div>
                <div class="panel-body">
                    <div class="dataTable_wrapper">
                        <form action="AddProduct.php" method="POST" role="form">
                                <div class="col-lg-6">
                                <div class="form-group">
                                    <label>Project Name</label>
                                    <select class='form-control' name='project'><option value=''>Select One</option><option value=''></option>    <option value=''></option><option value=''></option><option value=''></option><option value=''></option></select>                                    </div>
                                <div class="form-group">
                                    <label>Product Name</label>
                                    <input class="form-control" name="Product_name" type="text" id="Product_name" required="required" title="Enter Product Name">

                                </div>
                                <button type="submit" class="btn btn-primary" id="submit" data-toggle="modal" data-target="#myModal">Submit Query</button>
                                <button type="reset" class="btn btn-primary">Reset</button>
                                </div>

                                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">Particular Circle Will be included for selected Project</h4>
                                </div>
                                <div class="modal-body">
                                    Do you want to proceed??
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    <button type="submit" class="btn btn-primary" id="submit">Save changes</button>
                                </div>
                            </div>
                            <!-- /.modal-content -->
                        </div>
                        <!-- /.modal-dialog -->
                    </div>

                        </form>
                    </div>
                </div>

                    </div>
                </div>

                </div>   
            </div> 


</body>

3 个答案:

答案 0 :(得分:1)

试试这段代码     

<html>
    <?php
        include 'header.php';
       include 'footer.php';
        include './config.php';
    ?>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
       <div id="page-wrapper">
           <div class="row">
               <div class="col-lg-12">
                   <h1 class="page-header">Add Product</h1>
              </div>
              <!-- /.col-lg-12 -->
          </div>
          <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Product
                    </div>
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <form action="AddProduct.php" method="POST" role="form">
                                    <div class="col-lg-6">
                                    <div class="form-group">
                                        <label>Project Name</label>
                                        <?php 
                                            $sqll="SELECT * FROM project_master";
                                            echo "<select class='form-control' name='project'>";
                                            echo "<option value=''>Select One</option>";  
                                            foreach ($conn->query($sqll) as $row){
                                            echo "<option value=".$row['Project_id'].">".$row['Project_title']."</option>";  
                                            }
                                            echo "</select>";

                                        ?>
                                    </div>
                                    <div class="form-group">
                                        <label>Product Name</label>
                                        <input class="form-control" name="Product_name" type="text" id="Product_name" required="required" title="Enter Product Name">

                                    </div>
                                    <button type="submit" class="btn btn-primary" id="submit" data-toggle="modal" data-target="#myModal">Submit Query</button>
                                    <button type="reset" class="btn btn-primary">Reset</button>
                                    </div>

                                    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title" id="myModalLabel">Particular Circle Will be included for selected Project</h4>
                                    </div>
                                    <div class="modal-body">
                                        Do you want to proceed??
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary" id="submit">Save changes</button>
                                    </div>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>

                            </form>
                        </div>
                    </div>

                        </div>
                    </div>

                    </div>   
                </div> 


</body>

答案 1 :(得分:0)

我认为其中一个问题是您在echo $row[project_id]中缺少单引号,该引号应为$row['project_id']。 如果您将代码更改为以下内容会发生什么?

<?php 
    $sqll="SELECT * FROM project_master";
    echo "<select class='form-control' name='project'>";
    echo "<option value=''>Select One</option>"; 
    $results =  $conn->query($sqll);
    foreach ($results as $row) {
        echo "<option value=$row[Project_id]>$row[Project_title]</option>";  
    }
    echo "</select>";
?>

答案 2 :(得分:0)

正如@Karthikeyani所说,我有 替换此代码

$sqll="SELECT * FROM project_master"; $rows = $conn->query($sqll);
 echo "<select class='form-control' name='project'>";
echo "<option value=''>Select One</option>"; 
while($rows->fetch_assoc() as $row){
    echo "<option value=".$row['Project_id'].">".$row['Project_title']."</option>"; 
} 
echo "</select>"; ?>