首页 > 解决方案 > 在 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> 

即使我选择了两个或更多,我也只能显示一个用户的数据

编辑 :

这是我的脚本功能和显示功能:

<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 个或更多用户,但它只显示一个

标签: php

解决方案


这是我试图展示如何实现它。不显示表格数据。我没有回答确切的解决方案,因为我希望您学习基础知识。此代码仅作为示例。

$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

推荐阅读