Get a query from a table [closed]

0

Greetings, I have a list of names of a database in a table, to give in see details, I linked to another php file in which I would like to see the full details of that person and not leave the rest here I have the code of the details of the person, I do not know how to continue. Thanks:

 <?php
        include ("conexion.php");

     $query ="Select * from empleados where nombre=".$_POST["nombre"];
        $rows = $conexion->query($query);

        $row=$rows->fetch_assoc();
        ?>

        <p><strong>ID: </strong><?php echo $row["id"]?></p>
        <p><strong>nombre: </strong><?php echo $row["nombre"]?></p>
        <p><strong>apellido: </strong><?php echo $row["apellido"]?></p>
        <p><strong>dni: </strong><?php echo $row["dni"]?></p>
        <br/>

        <a href="index.php">Inicio</a>
        <?php
        $conexion = null;
        ?>
    
asked by paez 07.11.2017 в 19:25
source

1 answer

1

Your connection file I recommend it in the following way Conexion.php

<?php
try {
    $db = new PDO('mysql:host=localhost;dbname=tudb', 'root', '');
} catch (PDOException $e) {
    print "¡Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

Your other file whose name should not go like this:

<?php
$nombre = $_POST['nombre'];
$query = $db->prepare("SELECT * FROM empleados WHERE nombre = :nombre");
$query->bindParam(':nombre', $nombre);
$query->execute();
?>

<?php foreach ($query as $row): ?>
<p><strong>ID: </strong><?php echo $row["id"]?></p>
<p><strong>nombre: </strong><?php echo $row["nombre"]?></p>
<p><strong>apellido: </strong><?php echo $row["apellido"]?></p>
<p><strong>dni: </strong><?php echo $row["dni"]?></p>
<?php endforeach ?>
<br/>

<a href="index.php">Inicio</a>

It's up to you to modify it to your liking.

    
answered by 07.11.2017 в 20:28