2016-02-12 82 views
1

我想用2選擇選項()和()來過濾我的數據庫我想要的是當用戶加載頁面時,他們可以看到數據庫結果組織在表中,當他們選擇選擇(下拉列表)表格被過濾,他們可以選擇一個選擇(下拉列表)或兩者以獲得最佳結果。那麼如何調整我的代碼以按照我的要求工作?我使用oracle hr數據庫,但我不斷收到此錯誤:未定義的索引,可以告訴我爲什麼?使用選擇過濾數據庫

的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>"; 
} 
?> 
+0

你告訴它在同一時間發現與上級不同的價值觀的員工,這是身體上不可能:)。因此,它可能是要在查詢中使用不同的列來匹配$ office和$ report的值或....使用OR語句而不是AND? – Filip

+0

¿員工表如何? – Cuchu

+0

@Filip我的員工表具有以下屬性:employeeNumber lastName firstName extension officeCode email jobTitle,所以我不認爲查詢是問題,我認爲由於某種原因它不能識別$ office和$ report值,因爲當我回聲價值我得到了同樣的錯誤:未定義的索引,謝謝你的時間 – Glory

回答

1

試試這個代碼:兩點,我的評論數據庫連接和SQL(我的)需要加入到其他表,因爲你的數據或概念是錯誤的。在這種情況下,POST是空的,而不是相同的POST和GET,測試代碼並查看。

的index.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

+0

它是一樣的沒有結果表是空的它不能識別傳遞的值 – Glory

+0

我在我的桌面上測試..編輯mi回答 – Cuchu

+0

但是reportsTo不存在於表中,系統中是否存在其他表?或者什麼是報告在表中? – Cuchu