[英]Require multiple rows from database using multiple select in php
我有一個數據庫,其中包含用戶信息和(使用數據庫中的用戶名),我必須顯示數據庫中選擇的用戶名的所有信息。
<select name="user[]" id="user" multiple="multiple" tabindex="1" onchange="showUser(this.value)">
<?php while($row = mysqli_fetch_array($result)){
?>
<?php
foreach($result as $row){
?>
<option value ="<?php echo $row['username']; ?>"> <?php echo $row['username'];?></option>
<?php }
?>
<?php }?>
</select>
即使我 select 兩個或更多,我也只能顯示一個用戶的數據
編輯:
這是我的腳本 function 並顯示 function:
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","123.php?q="+str,true);
xmlhttp.send();
}
}
</script>
123.php:
<!DOCTYPE html>
<html>
<head>
<?php
session_start();
$q = $_REQUEST["q"];
require 'conectare.php';
mysqli_select_db($conectare,"users");
$sql = "Select * from users where username = '{$q}'";
$result = mysqli_query($conectare, $sql);
echo "<table>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
<th>email</th>
<th>Telefon</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['telefon'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conectare);
?>
</body>
</html>
看起來像這樣,我想顯示 2 個或更多用戶,但它只顯示一個
這是我試圖展示如何實現它。 不顯示表格數據。 我沒有回答確切的解決方案,因為我希望您學習基礎知識。 此代碼僅作為示例。
$connection = mysql_connect('localhost', 'root', '');
//The Blank string is the password
mysql_select_db('hrmwaitrose');
$query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){
//Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>";
//$row['index'] the index here is a field name
}
echo "</table>";
//Close the table in HTML
mysql_close();
//Make sure to close out the database connection
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.