将数据库中的数据显示到html表格中 [英] display data from database into html table

查看:162
本文介绍了将数据库中的数据显示到html表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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)
或die($ db - > error);

$ num = mysqli_num_rows($ run);
$ row = mysqli_fetch_array($ run,MYSQLI_ASSOC);
// $ yana = $ row ['staff_id'];
// echodd。$ yana;

echo< table>
< tr>
< td>员工ID< / td>
< td> / td>
< td>电子邮件< / td>
< td>角色< / 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>;
回声< / tr>;
echo< / table>;}

}
?>

html代码:

 < form id =list_of_usermethod =postaction =user_list.phpaccept-charset ='UTF-8'> 

< h2>表格示例< / h2>
< p>& nbsp;< / p>
< table width =729border =0>
< tr valign =center>
< td width =85valign =center>使用者:< / td>
< td width =196valign =center>< select name =type_user>
< option value =TELLERselected =selected> TELLER< / option>
< option value =MANAGER> MANAGER< / option>
< / select> < / TD>
< td width =97valign =center>< input name =Gotype =submitid =Govalue =Go/>< / td>
< / tr>
< / table>

我在一页中添加了php和html。最初,我有一个html表格准备好显示数据,但它不会显示出来。所以我把它改成了php。但页面在这里和那里。 。我在页面上使用了模板。



你能告诉我怎么做..。将数据从PHP传递给html?

解决方案

以下是PHP与数据库连接的解决方案总数html

 <!doctype html> 
< html lang =en>
< head>
< meta charset =UTF-8>
< title>数据库连接< / title>
< / head>
< body>
<?php
$ username =database-username;
$ password =database-password;
$ host =localhost;

$ connector = mysql_connect($ host,$ username,$ password)
或die(无法连接);
echo连接成功::;
$ selected = mysql_select_db(test_db,$ connector)
或die(Unable to connect);

//执行SQL查询并返回记录
$ result = mysql_query(SELECT * FROM table_one);
?>
< 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_dept'\]}< / td>
< td> {$ row \ ['employee_sal'\]}< / td>
< / TR> \\\
;
}
?>
< / tbody>
< / table>
<?php mysql_close($ connector); ?>
< / body>
< / html>

来源:


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

解决方案

Here is the solution total html with php and database connections

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

Source: retrieve data from db and display it in table in php .. see this code whats wrong with it?

这篇关于将数据库中的数据显示到html表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆