【问题标题】:How would I echo my results from a PHP search form into an HTML table? [closed]如何将 PHP 搜索表单中的结果回显到 HTML 表中? [关闭]
【发布时间】:2018-05-28 02:24:03
【问题描述】:

我希望我的结果采用 HTML 格式,例如:

[Submit]

结果:

______________
|ID|    NAME |
| 5|Ike      |
| 6|Bill     |
| 7|Michael  |
| 8|Kim      |
| 9|Stacey   |

这是我当前的 PHP 代码。为了让我的代码以表格格式显示,我需要做些什么不同的事情?

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();

if(!isset($_SESSION['username'])) {
   header("Location: index.php");
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
<link rel="stylesheet" href="datepicker.css" type="text/css" />
<link rel="stylesheet" href="demo.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="datepicker.js"></script>
<script type="text/javascript" src="ValidarDatos.js"></script>


<title>Consulta niño Coprodeli Intranet</title>
</head>
<body>


<div id="content">
    <div id="logo"> <a href="#"> <img src="images/logo.jpg" /></a> </div>
    <ul id="menu">
            <li><a href="home.php">Home</a></li>
            <li><a href="ConsultaNino.php" class="seleccionado">Niños</a></li>
            <li><a href="ConsultaPadrino.php">Padrinos</a></li>
            <li><a href="ConsultaCentro.php">Centros</a></li>
            <li><a href="Informes.php">Informes</a></li>            
            <li><a href="ConsultaUsuario.php">Usuarios</a></li>         
            <li><a href="Security/aut_logout.php">Salir</a></li>
    </ul>
    <div id="intro2">
        <h1>Intranet Coprodeli</h1>
        <p>Apadrinamiento y Programa<br />
            de Niños en Alto Riesgo</p>
        <div id="login">
            </div>
    </div>
    <div id="right2">
    <h4 class="migas">Estás en: <a href="home.php">Inicio</a> | Consulta de niños</h4>
    <br />


<?php
$output = NULL;
if(isset($_POST['submit'])){
    // Connect to the database
    $mysqli = NEW MySQLi("localhost","root","","coprodeli");
    $nino_id = $mysqli->real_escape_string( $_POST['nino_id']);
    $nombre = $mysqli->real_escape_string( $_POST['nombre']);
    $apellidos = $mysqli->real_escape_string( $_POST['apellidos']);
    $sexo = $mysqli->real_escape_string( $_POST['sexo']);
    $estado = $mysqli->real_escape_string( $_POST['estado']);
    $fecha_de_nacimiento_desde = $mysqli->real_escape_string( $_POST['fecha_de_nacimiento_desde']);
    $fecha_de_nacimiento_hasta = $mysqli->real_escape_string( $_POST['fecha_de_nacimiento_hasta']);
    $tipo_de_centro = $mysqli->real_escape_string( $_POST['tipo_de_centro']);
    $nombre_del_centro = $mysqli->real_escape_string( $_POST['nombre_del_centro']);
    $region_del_centro = $mysqli->real_escape_string( $_POST['region_del_centro']);
    $nivel_de_estudio = $mysqli->real_escape_string( $_POST['nivel_de_estudio']);
    $entrada_desde = $mysqli->real_escape_string( $_POST['entrada_desde']);
    $entrada_hasta = $mysqli->real_escape_string( $_POST['entrada_hasta']);
    $egreso_desde = $mysqli->real_escape_string( $_POST['egreso_desde']);
    $egreso_hasta = $mysqli->real_escape_string( $_POST['egreso_hasta']);
    //Query the database
    $resultSet = $mysqli->query("SELECT nino_id, nombre, apellidos, sexo, estado, fecha_de_nacimiento_desde, fecha_de_nacimiento_hasta,
    tipo_de_centro, nombre_del_centro, region_del_centro, nivel_de_estudio, entrada_desde, entrada_hasta, egreso_desde, egreso_hasta FROM nino
    WHERE nino_id = '$nino_id' AND nombre = '$nombre' AND apellidos = '$apellidos' AND sexo = '$sexo' AND estado = '$estado' AND fecha_de_nacimiento_desde = '$fecha_de_nacimiento_desde'
    AND fecha_de_nacimiento_hasta = '$fecha_de_nacimiento_hasta' AND tipo_de_centro = '$tipo_de_centro' AND nombre_del_centro = '$nombre_del_centro' AND
    region_del_centro = '$region_del_centro' AND nivel_de_estudio = '$nivel_de_estudio' AND entrada_desde = '$entrada_desde' AND entrada_hasta = '$entrada_hasta'
  AND egreso_desde = '$egreso_desde' AND egreso_hasta = '$egreso_hasta';" ) ;
    // Edited on 9/7/2015 12:39PM by Anthony Sawah. I changed it from  "  if($resultSet['num_rows'] > 0) {   "  to " if($resultSet->num_rows > 0) {"
    if($resultSet->num_rows > 0) {
        while($rows = $resultSet->fetch_assoc())
        {
            $nino_id = $rows['nino_id'];
            $nombre = $rows['nombre'];
            $apellidos = $rows['apellidos'];
            $sexo = $rows['sexo'];
            $estado = $rows['estado'];
            $fecha_de_nacimiento_desde = $rows['fecha_de_nacimiento_desde'];
            $fecha_de_nacimiento_hasta = $rows['fecha_de_nacimiento_hasta'];
            $tipo_de_centro = $rows['tipo_de_centro'];
            $nombre_del_centro = $rows['nombre_del_centro'];
            $region_del_centro = $rows['region_del_centro'];
            $nivel_de_estudio = $rows['nivel_de_estudio'];
            $entrada_desde = $rows['entrada_desde'];
            $entrada_hasta = $rows['entrada_hasta'];
            $egreso_desde = $rows['egreso_desde'];
            $egreso_hasta = $rows['egreso_hasta'];
            $output .= "Estado: $estado     ID niño: $nino_id   Apellidos: $apellidos     Nombre: $nombre   Fecha Ingreso: $egreso_desde  Fecha Egreso: $egreso_hasta  ";
        }
    }else{
        $output = "No results";
    }
}
?>
<fieldset class="required">
        <legend>Consulta de niños:</legend>
    <form method ="POST">
        ID niño: <input type="text" name="nino_id" />
        <br> </br>
        Nombre: <input type="text" name="nombre" />
        <br> </br>
        Apellidos: <input type="text" name="apellidos" />  <br> </br>
        Sexo: <input type="text" name="sexo" />  <br> </br>
        Estado: <input type="text" name="estado" />  <br> </br>
        Fecha de
        nacimiento desde
        (DD-MM-YYYY): <input type="text" name="fecha_de_nacimiento_desde" />  <br> </br>
        Fecha de
        nacimiento hasta
        (DD-MM-YYYY): <input type="text" name="fecha_de_nacimiento_hasta" />  <br> </br>
        Tipo de centro: <input type="text" name="tipo_de_centro" />  <br> </br>
        Nombre Del Centro: <input type="text" name="nombre_del_centro" />  <br> </br>
        Región del Centro: <input type="text" name="region_del_centro" />  <br> </br>
        Nivel de estudio: <input type="text" name="nivel_de_estudio" />  <br> </br>
        Entrada desde
        (DD-MM-YYYY): <input type="text" name="entrada_desde" />  <br> </br>
        Entrada hasta
        (DD-MM-YYYY): <input type="text" name="entrada_hasta" />  <br> </br>
        Egreso desde
        (DD-MM-YYYY): <input type="text" name="egreso_desde" />  <br> </br>
        Egreso hasta
        (DD-MM-YYYY): <input type="text" name="egreso_hasta" />  <br> </br>


        <input type="submit" name="submit" value="Search" />
    </form>
    </fieldset>



        <fieldset id="" class="required">
        <legend>Resultado de la b&uacute;squeda:</legend>
        <form id="form7" name="ResultadoBusqueda" action="ConsultarDetalleNino.php" enctype="multipart/form-data" method="post" >
        <table>

            <tr>
            <td class="especial" width="11"></td>
            <td colspan="6" class="especial"> </td>
            </tr>
                <tr>
                    <th></th>
                    <th width="15"></th>
                    <th width="88">Estado</th>
                    <th width="104">ID ninos </th>
                    <th width="234">Apellidos</th>
                    <th width="81">Nombre</th>
                    <th width="128">Fecha Ingreso</th>
                    <th width="106">Fecha Egreso</th>
                </tr>
            </table>


            <?php echo $output;?>
        <div class="actions">
          <input name="ConsultarDetalleNino" type="submit" class="primaryAction" id="submit-" value="Consultar Detalle">



            <input name="Cancelar" type="submit" class="primaryAction" id="submit-" value="Cancelar">
        </div>

        </form>
        <form id="ListadoNinos" name="ListadoNinos" action="InformeNinos.php"  method="post" target="_blank"></form>
        </fieldset>

    <div style="clear: both"></div>
</div>
<div id="footer">
    <div id="col1">
        <p><a href="#"> </a><a href="#"></a><br />
            &copy;  <a href="#"></a></p>
    </div>
    <div id="col2">
        <p>Info:<strong><a href=""></a></strong><br />
            Info2: <strong>completar</strong></p>
    </div>
    <div id="col3">
        <p>
    </div>

</div>
</div>
</body>
</html>

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:

    将您的 $output 更改为

    $output .= "<tr><td>".$estado."</td><td>".$nino_id."</td><td>".$apellidos."</td><td>".$nombre."</td><td>".$egreso_desde."</td><td>".$egreso_hasta."</td></tr>";
    

    更新 用下面的代码替换所有代码。

         <?php
    include_once 'includes/db_connect.php';
    include_once 'includes/functions.php';
    
    sec_session_start();
    
    if(!isset($_SESSION['username'])) {
       header("Location: index.php");
    }
    
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
    <link rel="stylesheet" href="datepicker.css" type="text/css" />
    <link rel="stylesheet" href="demo.css" type="text/css" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <script type="text/javascript" src="datepicker.js"></script>
    <script type="text/javascript" src="ValidarDatos.js"></script>
    
    
    <title>Consulta niño Coprodeli Intranet</title>
    </head>
    <body>
    
    
    <div id="content">
        <div id="logo"> <a href="#"> <img src="images/logo.jpg" /></a> </div>
        <ul id="menu">
                <li><a href="home.php">Home</a></li>
                <li><a href="ConsultaNino.php" class="seleccionado">Niños</a></li>
                <li><a href="ConsultaPadrino.php">Padrinos</a></li>
                <li><a href="ConsultaCentro.php">Centros</a></li>
                <li><a href="Informes.php">Informes</a></li>            
                <li><a href="ConsultaUsuario.php">Usuarios</a></li>         
                <li><a href="Security/aut_logout.php">Salir</a></li>
        </ul>
        <div id="intro2">
            <h1>Intranet Coprodeli</h1>
            <p>Apadrinamiento y Programa<br />
                de Niños en Alto Riesgo</p>
            <div id="login">
                </div>
        </div>
        <div id="right2">
        <h4 class="migas">Estás en: <a href="home.php">Inicio</a> | Consulta de niños</h4>
        <br />
    
    
    <?php
    $output = NULL;
    if(isset($_POST['submit'])){
        // Connect to the database
        $mysqli = NEW MySQLi("localhost","root","","coprodeli");
        $nino_id = $mysqli->real_escape_string( $_POST['nino_id']);
        $nombre = $mysqli->real_escape_string( $_POST['nombre']);
        $apellidos = $mysqli->real_escape_string( $_POST['apellidos']);
        $sexo = $mysqli->real_escape_string( $_POST['sexo']);
        $estado = $mysqli->real_escape_string( $_POST['estado']);
        $fecha_de_nacimiento_desde = $mysqli->real_escape_string( $_POST['fecha_de_nacimiento_desde']);
        $fecha_de_nacimiento_hasta = $mysqli->real_escape_string( $_POST['fecha_de_nacimiento_hasta']);
        $tipo_de_centro = $mysqli->real_escape_string( $_POST['tipo_de_centro']);
        $nombre_del_centro = $mysqli->real_escape_string( $_POST['nombre_del_centro']);
        $region_del_centro = $mysqli->real_escape_string( $_POST['region_del_centro']);
        $nivel_de_estudio = $mysqli->real_escape_string( $_POST['nivel_de_estudio']);
        $entrada_desde = $mysqli->real_escape_string( $_POST['entrada_desde']);
        $entrada_hasta = $mysqli->real_escape_string( $_POST['entrada_hasta']);
        $egreso_desde = $mysqli->real_escape_string( $_POST['egreso_desde']);
        $egreso_hasta = $mysqli->real_escape_string( $_POST['egreso_hasta']);
        //Query the database
        $resultSet = $mysqli->query("SELECT nino_id, nombre, apellidos, sexo, estado, fecha_de_nacimiento_desde, fecha_de_nacimiento_hasta,
        tipo_de_centro, nombre_del_centro, region_del_centro, nivel_de_estudio, entrada_desde, entrada_hasta, egreso_desde, egreso_hasta FROM nino
        WHERE nino_id = '$nino_id' AND nombre = '$nombre' AND apellidos = '$apellidos' AND sexo = '$sexo' AND estado = '$estado' AND fecha_de_nacimiento_desde = '$fecha_de_nacimiento_desde'
        AND fecha_de_nacimiento_hasta = '$fecha_de_nacimiento_hasta' AND tipo_de_centro = '$tipo_de_centro' AND nombre_del_centro = '$nombre_del_centro' AND
        region_del_centro = '$region_del_centro' AND nivel_de_estudio = '$nivel_de_estudio' AND entrada_desde = '$entrada_desde' AND entrada_hasta = '$entrada_hasta'
      AND egreso_desde = '$egreso_desde' AND egreso_hasta = '$egreso_hasta';" ) ;
        // Edited on 9/7/2015 12:39PM by Anthony Sawah. I changed it from  "  if($resultSet['num_rows'] > 0) {   "  to " if($resultSet->num_rows > 0) {"
        if($resultSet->num_rows > 0) {
            while($rows = $resultSet->fetch_assoc())
            {
                $nino_id = $rows['nino_id'];
                $nombre = $rows['nombre'];
                $apellidos = $rows['apellidos'];
                $sexo = $rows['sexo'];
                $estado = $rows['estado'];
                $fecha_de_nacimiento_desde = $rows['fecha_de_nacimiento_desde'];
                $fecha_de_nacimiento_hasta = $rows['fecha_de_nacimiento_hasta'];
                $tipo_de_centro = $rows['tipo_de_centro'];
                $nombre_del_centro = $rows['nombre_del_centro'];
                $region_del_centro = $rows['region_del_centro'];
                $nivel_de_estudio = $rows['nivel_de_estudio'];
                $entrada_desde = $rows['entrada_desde'];
                $entrada_hasta = $rows['entrada_hasta'];
                $egreso_desde = $rows['egreso_desde'];
                $egreso_hasta = $rows['egreso_hasta'];
                $output .= "<tr><td>".$estado."</td><td>".$nino_id."</td><td>".$apellidos."</td><td>".$nombre."</td><td>".$egreso_desde."</td><td>".$egreso_hasta."</td></tr>";
            }
        }else{
            $output = "No results";
        }
    }
    ?>
    <fieldset class="required">
            <legend>Consulta de niños:</legend>
        <form method ="POST">
            ID niño: <input type="text" name="nino_id" />
            <br> </br>
            Nombre: <input type="text" name="nombre" />
            <br> </br>
            Apellidos: <input type="text" name="apellidos" />  <br> </br>
            Sexo: <input type="text" name="sexo" />  <br> </br>
            Estado: <input type="text" name="estado" />  <br> </br>
            Fecha de
            nacimiento desde
            (DD-MM-YYYY): <input type="text" name="fecha_de_nacimiento_desde" />  <br> </br>
            Fecha de
            nacimiento hasta
            (DD-MM-YYYY): <input type="text" name="fecha_de_nacimiento_hasta" />  <br> </br>
            Tipo de centro: <input type="text" name="tipo_de_centro" />  <br> </br>
            Nombre Del Centro: <input type="text" name="nombre_del_centro" />  <br> </br>
            Región del Centro: <input type="text" name="region_del_centro" />  <br> </br>
            Nivel de estudio: <input type="text" name="nivel_de_estudio" />  <br> </br>
            Entrada desde
            (DD-MM-YYYY): <input type="text" name="entrada_desde" />  <br> </br>
            Entrada hasta
            (DD-MM-YYYY): <input type="text" name="entrada_hasta" />  <br> </br>
            Egreso desde
            (DD-MM-YYYY): <input type="text" name="egreso_desde" />  <br> </br>
            Egreso hasta
            (DD-MM-YYYY): <input type="text" name="egreso_hasta" />  <br> </br>
    
    
            <input type="submit" name="submit" value="Search" />
        </form>
        </fieldset>
    
    
    
            <fieldset id="" class="required">
            <legend>Resultado de la b&uacute;squeda:</legend>
            <form id="form7" name="ResultadoBusqueda" action="ConsultarDetalleNino.php" enctype="multipart/form-data" method="post" >
            <table>
    
                <tr>
                <td class="especial" width="11"></td>
                <td colspan="6" class="especial"> </td>
                </tr>
                    <tr>
                        <th></th>
                        <th style="width:15px"></th>
                        <th style="width:88px">Estado</th>
                        <th style="width:104px">ID ninos </th>
                        <th style="width:234px">Apellidos</th>
                        <th style="width:81px">Nombre</th>
                        <th style="width:128px">Fecha Ingreso</th>
                        <th style="width:106px">Fecha Egreso</th>
                    </tr>
    
                    <?php echo $output;?>
    
                </table>
    
    
                <?php echo $output;?>
            <div class="actions">
              <input name="ConsultarDetalleNino" type="submit" class="primaryAction" id="submit-" value="Consultar Detalle">
    
    
    
                <input name="Cancelar" type="submit" class="primaryAction" id="submit-" value="Cancelar">
            </div>
    
            </form>
            <form id="ListadoNinos" name="ListadoNinos" action="InformeNinos.php"  method="post" target="_blank"></form>
            </fieldset>
    
        <div style="clear: both"></div>
    </div>
    <div id="footer">
        <div id="col1">
            <p><a href="#"> </a><a href="#"></a><br />
                &copy;  <a href="#"></a></p>
        </div>
        <div id="col2">
            <p>Info:<strong><a href=""></a></strong><br />
                Info2: <strong>completar</strong></p>
        </div>
        <div id="col3">
            <p>
        </div>
    
    </div>
    </div>
    </body>
    </html>
    

    【讨论】:

    • 太棒了!我看到你在那里做了什么。您刚刚添加了标签。它现在打印出结果,但没有间距。我可以为间距添加任何参数吗?如: Estado ID ninos Apellidos Nombre Fecha Ingreso Fecha Egreso
    • 你可以做style="width:15px"
    • 嗯,我正在这样做,但出现错误。你能举个例子说明我到底把它放在哪里吗?抱歉,我是 PHP 新手
    • 我更新了css样式。这就是你要找的吗?
    • 我更新了答案。用那个替换你的所有代码。这应该可以解决您的问题。让我知道是否有。 @AnthonySawah(如果有任何错误,请告诉我它们是什么。谢谢)
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签