-1

I'm trying to display data from database into a table in html. here is my code:

php code:

if($_SERVER['REQUEST_METHOD'] =='POST')
{   
 $type_user=$_POST['type_user'];
 $sql="SELECT staff_id, name, email, role FROM user WHERE role='$type_user'";

 $run= $db->query($sql)
    or die($db -> error);

 $num=mysqli_num_rows($run);
 $row=mysqli_fetch_array($run, MYSQLI_ASSOC);
 //$yana =  $row['staff_id'];
 //echo "dd".$yana;

 echo "<table >
  <tr>
    <td >Staff ID </td>
    <td >Name</td>
    <td >Email</td>
    <td >Role</td>
  </tr>";

  while($row = mysqli_fetch_array($run, MYSQLI_ASSOC))
  {
  echo "<tr>";
  echo "<td>".$row['staff_id']."</td>";
  echo "<td>".$row['name']."</td>";
  echo "<td>".$row['email']."</td>";
  echo "<td>".$row['role']."</td>";
  echo "</tr>";
  echo "</table>";}

 }
 ?>

html code:

<form id="list_of_user"  method="post" action="user_list.php" accept-charset='UTF-8'>

<h2> Table Example</h2>
<p>&nbsp;</p>
<table width="729" border="0" >
  <tr valign ="center">
    <td width="85" valign ="center">User: </td>
    <td width="196" valign ="center"><select name="type_user">
      <option value="TELLER" selected="selected">TELLER</option>
      <option value="MANAGER">MANAGER</option>
    </select>          </td>
    <td width="97" valign ="center"><input name="Go" type="submit" id="Go" value="Go"     /></td>
  </tr>
  </table>

I have php and html in one page.

originally, I have a html table ready to display the data, but it won't show up. so I changed it into php. but the page goes here and there. . i'm using template for the page.

Can you please show me how to ..say. pass the data from php to html??

yivi
  • 42,438
  • 18
  • 116
  • 138
lelouchliana
  • 33
  • 2
  • 2
  • 8

1 Answers1

1

You must take echo "</table>"; out of the while loop.

MillaresRoo
  • 3,808
  • 1
  • 31
  • 37