编辑:有额外问题"或者在添加第二个select子句时使用某种语法。
所以我正在使用mysql adventureworks示例数据库开发一个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> <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>
我理解在第二个文件中需要更改的内容才能获得我想要的结果但是我只是在第一个文件中填充第二个下拉列表时遇到了麻烦。 任何帮助,将不胜感激。感谢。
答案 0 :(得分:0)
你错过了search.php中的双引号
// Put genders in the options
foreach($stmt->fetchAll() as $genders) {
echo "<option value ='" . $genders['gender'] . "'>" .
$genders['gender'] . "</option>\n";
}