gpt4 book ai didi

javascript - 使用 select 过滤数据库

转载 作者:行者123 更新时间:2023-11-28 23:37:27 27 4
gpt4 key购买 nike

我正在尝试使用 2 select option() 和 () 来过滤我的数据库,我想要的是当用户加载页面时,当他们选择选择(下拉列表)时,他们可以看到组织在表中的数据库结果) 表被过滤,他们可以选择其中一个选择(下拉列表)或两个选择以获得最佳结果。那么我怎样才能调整我的代码以按我想要的方式工作呢?我正在使用 oracle hr 数据库,但我不断收到此错误:Undefined index,谁能告诉我为什么?

索引.php

<html>
<head>
</head>
<body>
<form action="emp.php" method="post" name="Form2" id="Form2">
<select id="officecode" name="officecode">
<option value="">Select an officeCode:</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="reportsTo" name="reportsTo" >
<option value="">reports To:</option>
<option value="1143">1143</option>
<option value="1102">1102</option>
<option value="1108">1108</option>
<option value="1056">1056</option>
</select>
</form>
<br>
<div id="result"><b>
<?php include "emp.php"; ?>
</b></div>
<script>
$(document).ready(function(){
var office = $("#officecode");
var report = $("#reportsTo");
the_office.change(function(){
var the_selected_office = $(this).val();
self.location = "emp.php?off="+the_selected_office+"&rep=";
});
the_report.change(function(){
var the_selected_report = $(this).val();
self.location = "emp.php?off=&rep="+the_selected_report+";
});
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</body>
</html>

emp.php

<?php
$office = $_POST["off"];
$report = $_POST["rep"];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "classicmodels";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql="SELECT * FROM employees WHERE reportsTo= '".$office."' AND reportsTo= '".$report."' ";
$result = $conn->query($sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Employee Number</th>
<th>Extension</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['employeeNumber'] . "</td>";
echo "<td>" . $row['extension'] . "</td>";
echo "</tr>";
}
?>

最佳答案

试试这个代码:两点,我评论数据库连接和sql(我的)需要连接到其他表,因为你的数据或概念是错误的。本例中POST为空,POST和GET不一样,测试代码看看。

索引.php

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

$(document).ready(function(){

var office = $("#officecode");
var report = $("#reportsTo");

$("#officecode").change(function(){
var the_selected_office = $(this).val();
self.location = "index.php?off="+the_selected_office+"&rep=";
});

$("#reportsTo").change(function(){
var the_selected_report = $(this).val();
self.location = "index.php?off=&rep="+the_selected_report+"";
});


});

</script>

</head>
<body>
<select id="officecode" name="officecode">
<option value="">Select an officeCode:</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<select id="reportsTo" name="reportsTo" >
<option value="">reports To:</option>
<option value="1143">1143</option>
<option value="1102">1102</option>
<option value="1108">1108</option>
<option value="1056">1056</option>
</select>
<br>
<div id="result"><b>
<?php include "emp.php"; ?>
</b></div>
</body>
</html>

emp.php

<?php

print_r("<pre>");
print_r($_GET);
print_r($_POST);
print_r("</pre>");

/*if(isset($_GET["off"]) || isset($_GET["rep"])) {*/

$office = $_GET["off"];
$report = $_GET["rep"];

$where = array();
if(!empty($office)) {
$where[] = "officeCode = {$office}";
}

if(!empty($report)) {
$where[] = "reportsTo = {$report}";
}


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "classicmodels";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}



if($where) {
$w = implode(" OR ", $where);
} else {
$w = 1;
}

$sql="SELECT * FROM employees WHERE {$w}";
print_r("<pre>");
print_r($sql);
print_r("</pre>");
$result = $conn->query($sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Employee Number</th>
<th>Extension</th>

</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['employeeNumber'] . "</td>";
echo "<td>" . $row['extension'] . "</td>";

echo "</tr>";
}

/*}*/
?>

Empty filter OfficeCode = 3

关于javascript - 使用 select 过滤数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com