我正在尝试将数据库中的数据显示为html中的表格。这是我的代码:
php代码:
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代码:
<form id="list_of_user" method="post" action="user_list.php" accept-charset='UTF-8'>
<h2> Table Example</h2>
<p> </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>
我在一个页面中有php和html。
最初,我有一个html表准备显示数据,但它不会显示。所以我把它改成了php。但页面到处都是。 。我正在使用页面模板。
你可以告诉我怎么样的.say。将数据从php传递给html ??答案 0 :(得分:7)
以下是使用php和数据库连接的解决方案总html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "database-username";
$password = "database-password";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_one ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Employee_id</th>
<th>Employee_Name</th>
<th>Employee_dob</th>
<th>Employee_Adress</th>
<th>Employee_dept</th>
<td>Employee_salary</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row\['employee_id'\]}</td>
<td>{$row\['employee_name'\]}</td>
<td>{$row\['employee_dob'\]}</td>
<td>{$row\['employee_addr'\]}</td>
<td>{$row\['employee_dept'\]}</td>
<td>{$row\['employee_sal'\]}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
来源:retrieve data from db and display it in table in php .. see this code whats wrong with it?
答案 1 :(得分:0)
您必须从while循环中取出echo "</table>";
。