how to put mysql data in a table

-2
<html lang="en">
<head>
  <title>Consultar Ausencias</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<h3 style='text-align=center'>Tabla de ausencias</h3>


<?php

    //CONECTARSE A LA BASE DE DATOS
    require_once 'dbconnect.php';

    //SCRIPT DE LA CONSULTA SQL
    $q ="SELECT * FROM h_guardias;";
    //EJECUTAR LA CONSULTA
    $r= mysqli_query($conn,$q);
    while($row = mysqli_fetch_array($r))


    {

        echo "<tr>";
        echo "<tr>" .$row ['Nombre'] . "</td>";
        echo "<tr>" .$row ['Apellidos'] . "</td>";
        echo "<tr>" .$row ['Motivo'] . "</td>";
        echo "<tr>" .$row ['Dia'] . "</td>";
        echo "<tr>" .$row ['Hora'] . "</td>";
        echo "<tr>" .$row ['Asignatura'] . "</td>";
        echo "<tr>" .$row ['Ejercicios'] . "</td>";
    }
    ?>


<table class="table table-bordered"> 

    <thead>
        <tr>
            <th>Nombre </th>
            <th>Apellidos </th>
            <th>Motivo </th>
            <th>Dia </th>
            <th>Hora</th>
            <th>Asignatura</th>
            <th>Ejercicios</th>
        </tr>
        <tr>
            <td>SELECT*FROM .$row ['Nombre'] . </td>
        </tr>   
    </thead>
    <tbody id= "myTable">
    </tbody>
    </table>


</body>
</html>
    
asked by lolai 01.03.2018 в 17:30
source

2 answers

0

The information you provide is not correct, so I'll show you a code that you take from the php documentation I do not know if you use PDO or mysli.

<?php 
    include_once "conexion.php";
    try {  
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      $pdo->beginTransaction();
      $pdo->exec("insert into users (name, last_name, email, password) values ('Joe', 'Bloggs', '[email protected]','123445')");
      echo "Datos agregados! :)";
      $pdo->commit();

    } catch (Exception $e) {
      $pdo->rollBack();
      echo "Fallo: " . $e->getMessage();
    }

 ?>

Of course once the connection is made you include it in your project. in this case when calling this php file, I run the script. my database provider, in my case it is Postgresql. using PDO .

DB postgresql you can look for more examples. in the php documentation

I hope I help you.

    
answered by 01.03.2018 в 17:41
0

You are well oriented, what you need is to change the place where the body of your lines are printed, the generic form of popular a table with data in php would be the following

<?php

require_once 'dbconnect.php';

//SCRIPT DE LA CONSULTA SQL
$q = "SELECT * FROM h_guardias;";
//EJECUTAR LA CONSULTA
$query = mysqli_query($conn, $q);
?>
<table class="data-table">
    <thead>
    <tr>
        <th>Nombre </th>
        <th>Apellidos </th>
        <th>Motivo </th>
        <th>Dia </th>
        <th>Hora</th>
        <th>Asignatura</th>
        <th>Ejercicios</th>
    </tr>
    </thead>
    <tbody>
    <?php
    while ($row = mysqli_fetch_array($query)) {
        echo '<tr>
                 <td>' . $row ['Nombre'] . '</td>
                 <td>' . $row ['Apellidos'] . '</td>
                 <td>' . $row ['Motivo'] . '</td>
                 <td>' . $row ['Dia'] . '</td>
                 <td>' . $row ['Hora'] . '</td>
                 <td>' . $row ['Asignatura'] . '</td>
                 <td>' . $row ['Ejercicios'] . '</td>
                </tr>';
    } ?>
    </tbody>
</table>
    
answered by 01.03.2018 в 18:05