How to display a MySQL table in HTML?

1

I have a problem linking a DB and displaying it in an HTML page in a table.

I made the connection to the database in the following way:

<?php 
$connect = mysql_connect("localhost", "username", "password") or die(mysql_error()); 
mysql_select_db("a6113712_IESPADB", $connect) or die(mysql_error()); 
?>

The code of the button is as follows

<?php
include"connexion.php";

if(is set($_POST['search'])) {

    $sql = "SELECT * FROM alumnos ";
    $search_termn =  mysql_real_escape_string("POST"['Nombre']);
    $search_termp =  mysql_real_escape_string("POST"['ApeidoP']);
    $search_termm =  mysql_real_escape_string("POST"['ApeidoM']);
    $search_termc =  mysql_real_escape_string("POST"['Cuip']);
    $sql .=" WHERE (NOMBRE = '{$search_termn}') AND (ApeidoPaterno = {$search_termp}') AND (ApeidoMaterno = {$search_termm}' ) ";
}

$query = mysql_query($sql) or die(mysql_error());

?>
<!--<<form name="search_form" method="POST" action="display.php">
Search: <input type="text" name="search_box" value="" />
<input type="submit" name="search" value="search the table..">
</form>-->

<table width="70%" cellpadding="5" cellspace="5">

<tr>
    <td><strong>NombreDeClase</strong></td>
    <td><strong>IDT</strong></td>
    <td><strong>CUIP</strong></td>
    <td><strong>NOMBRE</strong></td>
    <td><strong>ApeidoPaterno</strong></td>
    <td><strong>ApeidoMaterno</strong></td>
    <td><strong>DirigidoA</strong></td>
    <td><strong>Docente</strong></td>
    <td><strong>AñoDeInicio</strong></td>
    <td><strong>MesDeInicio</strong></td>
    <td><strong>DiaDeInicio</strong></td>
    <td><strong>AñoDeTerminacion</strong></td>
    <td><strong>MesDeTerminacion</strong></td>
    <td><strong>DiaDeTerminacion</strong></td>
    <td><strong>HorasCursadas</strong></td>
    <td><strong>CA</strong></td>
    <td><strong>NO</strong></td>
    <td><strong>Cargo</strong></td>
    <td><strong>Procedencia</strong></td>
    <td><strong>Dependencia</strong></td>
    <td><strong>Libro</strong></td>
    <td><strong>Foja</strong></td>
    <td><strong>Califiaciones</strong></td>
</tr>
<?php while ($row = mysql_fetch_array($query)) {?>
    <td><?php echo $row['NombreDeClase'] ?></td>
    <td><?php echo $row['IDT'] ?></td>
    <td><?php echo $row['CUIP'] ?></td>
    <td><?php echo $row['NOMBRE'] ?></td>
    <td><?php echo $row['ApeidoPaterno'] ?></td>
    <td><?php echo $row['ApeidoMaterno'] ?></td>
    <td><?php echo $row['DirigidoA'] ?></td>
    <td><?php echo $row['Docente'] ?></td>
    <td><?php echo $row['AñoDeInicio'] ?></td>
    <td><?php echo $row['MesDeInicio'] ?></td>
    <td><?php echo $row['DiaDeInicio'] ?></td>
    <td><?php echo $row['AñoDeTerminacion'] ?></td>
    <td><?php echo $row['MesDeTerminacion'] ?></td>
    <td><?php echo $row['DiaDeTerminacion'] ?></td>
    <td><?php echo $row['HorasCursadas'] ?></td>
    <td><?php echo $row['CA'] ?></td>
    <td><?php echo $row['NO'] ?></td>
    <td><?php echo $row['Cargo'] ?></td>
    <td><?php echo $row['Procedencia'] ?></td>
    <td><?php echo $row['Dependencia'] ?></td>
    <td><?php echo $row['Libro'] ?></td>
    <td><?php echo $row['Foja'] ?></td>
    <td><?php echo $row['Califiaciones'] ?></td>
<?php} ?>

</table>
    
asked by Jose Carlos Rodriguez 07.07.2016 в 16:27
source

1 answer

1

There is an error in the SQL query, you are missing a couple of quotes that will make everything fail:

$sql .=" WHERE (NOMBRE = '{$search_termn}') AND (ApeidoPaterno = {$search_termp}') AND (ApeidoMaterno = {$search_termm}' ) ";

It should be this way:

$sql .=" WHERE (NOMBRE = '{$search_termn}') AND (ApeidoPaterno = '{$search_termp}') AND (ApeidoMaterno = '{$search_termm}' ) ";

Note that I have added the opening quotes for {$search_termp} and {$search_termm} . Ideally, it would be better to use prepared queries that will avoid possible problems if the values contain quotes (and will save you headaches with SQL injection vulnerabilities).

Apart from that, you have to add rows in the table, right now you are only adding cells in the loop, which will make them all appear in the same row. Try changing the code to this:

<?php while ($row = mysql_fetch_array($query)) {?>
<tr>
    <td><?php echo $row['NombreDeClase'] ?></td>
    <td><?php echo $row['IDT'] ?></td>
    <td><?php echo $row['CUIP'] ?></td>
    <td><?php echo $row['NOMBRE'] ?></td>
    <td><?php echo $row['ApeidoPaterno'] ?></td>
    <td><?php echo $row['ApeidoMaterno'] ?></td>
    <td><?php echo $row['DirigidoA'] ?></td>
    <td><?php echo $row['Docente'] ?></td>
    <td><?php echo $row['AñoDeInicio'] ?></td>
    <td><?php echo $row['MesDeInicio'] ?></td>
    <td><?php echo $row['DiaDeInicio'] ?></td>
    <td><?php echo $row['AñoDeTerminacion'] ?></td>
    <td><?php echo $row['MesDeTerminacion'] ?></td>
    <td><?php echo $row['DiaDeTerminacion'] ?></td>
    <td><?php echo $row['HorasCursadas'] ?></td>
    <td><?php echo $row['CA'] ?></td>
    <td><?php echo $row['NO'] ?></td>
    <td><?php echo $row['Cargo'] ?></td>
    <td><?php echo $row['Procedencia'] ?></td>
    <td><?php echo $row['Dependencia'] ?></td>
    <td><?php echo $row['Libro'] ?></td>
    <td><?php echo $row['Foja'] ?></td>
    <td><?php echo $row['Califiaciones'] ?></td>
</tr>
<?php} ?>
    
answered by 08.07.2016 в 18:01