0

I have a database witch contain users information and (with user name from database) and I have to show all information from database for user name selected.

<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> 

I can only display data for one user even if I select two or more

edit :

this is my script function and display 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>

enter image description here

It looks like this, I want to display 2 or more users and it only shows me one

  • Now is the time to do some debugging. In your browser's debugging tools, observe the AJAX request being made. Does it contain the data you expect? In your server-side code, what is the runtime value of `$sql` being executed? Is it what you expect? (Note: Your code is *wide open* to **SQL injection**. You'll want to correct that.) – David Jun 10 '21 at 11:17

2 Answers2

0

Here's I am trying to show how you can achieve it. To display tabular data without. I am not answering the exact solution, because i want you learn the basics. this code is for example only.

$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
Prahsant Sharma
  • 371
  • 4
  • 13
  • I would really appreciate if you would show me the exact solution, because I really need to finish it until tomorrow. I have to use – I like to play games Jun 10 '21 at 12:12
0

You can open your browser tools, go to network and send the request, select the XHR pill and observe your request in the preview/response tab of the request. Also a good practice is to use parameterized queries. This web page is actually asking for someone to perform an SQL Injection.

The preview of your actual data will be really helpful in loading the page data to see what is the error that you are getting.

runtimeTerror
  • 398
  • 3
  • 14