1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Formulaire de recherche</title>
</head>
<body>
<form method="GET">
<input type="search" name="terme">
<input type="submit" name="submit" value="Rechercher">
</form>
</body>
</html>
<?php
if (isset($_GET['submit'])) {
if (!empty($_GET['terme'])) {
// Connexion à la base de donnée.
$servername = "localhost";
$dbname = "test";
$username = "root";
$password = "";
try {
$con = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$terme = htmlspecialchars($_GET['terme']);
// On récupère les données de la table.
$stmt = $con->prepare(' SELECT *
FROM produits
WHERE
refInterne LIKE ? OR
refConstructeur LIKE ? OR
refGrossiste LIKE ?
');
$stmt->execute(['%' . $terme . '%', '%' . $terme . '%', '%' . $terme . '%']);
// Vérification que le terme recherché se trouve dans nos colonnes.
if ($stmt->rowCount() == 1) {
?>
<table>
<tr>
<th>Ref Interne</th>
<th>Ref Constructeur</th>
<th>Ref Grossiste</th>
</tr>
<?php
// On récupère toutes les références associé au terme recherché.
while ($donnees = $stmt->fetch()) {
?>
<tr>
<td><?php echo $donnees['refInterne']; ?></td>
<td><?php echo $donnees['refConstructeur']; ?></td>
<td><?php echo $donnees['refGrossiste']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo '<p>Valeur non trouvée.</p>';
}
} else {
echo '<p>Vous n\'avez entrer aucune valeur de recherche.</p>';
}
$stmt = null;
}
?> |
Partager