如何使用不同的表列用mysql/php填充多个下拉框

fzwojiic  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(219)

编辑:在添加第二个select子句时,对extra“或某种语法有问题。
所以我在使用mysql冒险家样例数据库进行php项目。当前,此页面接收一个员工标题,您可以使用包含所有可能标题的下拉框从中选择该标题以筛选员工。然而,我想添加第二个框与性别,以便有人可以通过过滤标题和性别,并获得所有可能的员工。
我试过一些方法,但通常都会导致页面崩溃。
这里是第一个文件:search.php

<?php
   // Connect to the database

   if (!include('connect.php')) {
      die('error finding connect file');
   }

   $dbh = ConnectDB();
?>
<html>

<head>
   <link href='http://fonts.googleapis.com/css?family=Cuprum' 
rel='stylesheet' type='text/css'>
   <link href="https://fonts.googleapis.com/css?family=Amaranth" 
rel="stylesheet">
   <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" 
rel="stylesheet">
         <link href="main.css" rel="stylesheet">
   <title>Table Results</title>
</head>

<body>
<div class="content">

<h1>Search Adventureworks Employees</h1>
<p>Select two parameters and search information from the
AdventureWorks Database.</p>

<div class="form">
<form action="listTable.php" method="get">

<?php

   // Get the full list of titles from Employee Table
  $sql =  "SELECT Distinct title FROM adventureworks.employee";
  $stmt = $dbh->prepare($sql);
  $stmt->execute();

 // Prep drop down control
  echo "<label for='title'>Select title: </label>\n";
  echo "<select id='title' name='title'>\n";

 // Put titles in the options
  foreach ($stmt->fetchAll() as $titles) {
     echo "<option value='" . $titles['title'] . "'>" .
$titles['title'] . "</option>\n";
  }
   // End dropdown
  echo "</select>";

  // Get the full list of genders from employee table
  $sql3 = "Select Distinct gender FROM adventureworks.employee";
  $stmt = $dbh->prepare($sql3);
  $stmt->execute();

  //Prep dropdown
  echo "<label for ='gender'>Select Gender: </label>\n";
  echo "<select id='gender' name = 'gender'>\n";

  // Put genders in the options
  foreach($stmt->fetchAll() as $genders) {
     echo "<option value ='" . $genders['gender'] . "'> .
$genders['gender'] . "</option>\n";
   }
  //end dropdown and submit
  echo "</select>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' 
value='Submit'>\n</form>\n</div>";
?>

</div>

</body>
</html>

下面是第二个php文件:listtable.php

<?php
   // Connect to the database

   if (!include('connect.php')) {
      die('error finding connect file');
   }

   $dbh = ConnectDB();
?>
<html>

<head><link href='http://fonts.googleapis.com/css?family=Cuprum' 
rel='stylesheet' type='text/css'>
   <link href="https://fonts.googleapis.com/css?family=Amaranth" 
rel="stylesheet">
   <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" 
rel="stylesheet">
     <link href="main.css" rel="stylesheet">
   <title>Table Results</title>
</head>

 <body>
 <div class='content'>

<?php
 // Get table name from querystring
    if (!isset($_GET["title"])) {
      echo "No Title Selected";
    }
   else {

      $title=$_GET["title"];
      echo "<h1>Listing of Employees</h1>\n";

      $sql1 = "SELECT column_name FROM information_schema.columns ";
      $sql1 .= "WHERE table_name = 'employee'";
      $stmt = $dbh->prepare($sql1);
      $stmt->execute();
      $cols = $stmt->rowCount();

  // Prep table
  $tableHTML = "<table>\n<thead>\n<tr>\n";

  // Table headings (column names)
  foreach ($stmt->fetchAll() as $columns) {
     $tableHTML .= "<th>" . $columns['column_name'] . "</th>\n";
  }

  // Prep table body
  $tableHTML .= "</tr>\n</thead>\n<tbody>\n";

  // Table body (column values)
  $sql2 = "SELECT * FROM adventureworks.employee e";
  $sql2 .= " Where title like '$title'";
  $stmt = $dbh->prepare($sql2);
  $stmt->execute();
  echo $stmt->rowCount() . " rows retrieved<br/><br />\n";

  foreach ($stmt->fetchAll() as $rows ) {
     $tableHTML .= "<tr>\n";
     for ($i = 0; $i < $cols; $i++) {
        $tableHTML .= "<td>" . $rows[$i] . "</td>\n";
     }
     $tableHTML .= "</tr>\n";
  }

  // End table
  $tableHTML .= "</tbody>\n</table>\n";
  echo $tableHTML;
  echo "<div class='code'><br />" . $sql1 . "<p>" . $sql2 . "</div>\n";
 } 
?>

</div>
</body>
</html>

我知道为了得到我想要的结果,我必须在第二个文件中更改什么,但是我在第一个文件中填充第二个下拉列表时遇到了问题。任何帮助都将不胜感激。谢谢。

nukf8bse

nukf8bse1#

您在search.php中错过了一个双引号

// Put genders in the options
  foreach($stmt->fetchAll() as $genders) {
     echo "<option value ='" . $genders['gender'] . "'>" .
    $genders['gender'] . "</option>\n";
   }

相关问题