簡體   English   中英

在 php 中使用多個 select 需要數據庫中的多行

[英]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

您可以打開瀏覽器工具 go 到網絡並發送請求 select XHR葯丸並在請求的預覽/響應選項卡中觀察您的請求。 一個好的做法是使用參數化查詢 這個 web 頁面實際上是在要求某人執行SQL 注入

實際數據的預覽對於加載頁面數據以查看您遇到的錯誤非常有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM