Propozycja
index.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { // 1
if (! empty($_POST['searched_word'])) { // 2
$searched_word = trim($_POST['searched_word']);
if (strlen($searched_word) >= 3) { // 3
try { // 4
require_once('db_functions.php');
$search = search($searched_word);
if ($search != null) { // 5
$search_result = '';
foreach ($search as $data) { // 6
$search_result .= '<div class="search-result-row">'
.' <span class="search-result-title">'.$data['nazwa_kolumny_1'].'</span><br>'
.' <span class="search-result-description">'.$data['nazwa_kolumny_2'].'</span>'
.'</div>';
} // 6
} else { // 5
$search_result = '<div class="search-result-empty">'
."Dla szukanego słowa: <b>{$searched_word}</b> - nie znaleziono żadnych wyników"
.'</div>';
}
} catch (Exception $e) { // 4
$search_result = $e->getMessage();
}
} else { // 3
$search_result = 'Szukane słowo powinno zawierać min. 3 znaki';
}
} else { // 2
$search_result = '';
}
} else { // 1
$search_result = '';
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
border: 0;
}
/* Css do demonstracji */
.container {
width: 95vw;
margin: 0 auto;
font: 1em/1.4em monospace;
}
input, label {
margin: 0.5em;
}
input[type="submit"] {
cursor: pointer;
}
input[type="text"] {
padding: 0.2em;
}
input[type="text"]:focus {
outline: none;
box-shadow: 0 0 4px rgba(0,128,0,1);
}
input[type="text"]::-webkit-input-placeholder {
font: 0.8em monospace;
color: gray;
transition: all 0.5s;
}
input[type="text"]:focus::-webkit-input-placeholder {
word-spacing: 2em;
color: rgba(128,128,128,0);
padding-left: 2em;
}
.search-result-container {
margin-top: 1em;
}
.search-result-row {
/* Twój css */
}
.search-result-row .search-result-title {
/* Twój css */
}
.search-result-row .search-result-description {
/* Twój css */
}
.search-result-empty {
/* Twój css */
color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="search">
<form action="" method="post">
<label for="searched_word">Szukaj</label>
<input type="text" name="searched_word" id="searched_word" placeholder="Wpisz szukane słowo" required>
<input type="submit" value="Szukaj">
</form>
</div>
<div class="search-result-container">
<?php echo $search_result; ?>
</div>
</div>
</body>
</html>
db_functions.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','nazwa_bazy');
function connect($conn = null) {
if ($conn == null) {
$conn = mysqli_connect(HOST,USER,PASS,DB) or die("Błąd połączenia!");
return $conn;
} else {
$conn->close();
}
}
function search($word) {
$conn = connect();
$sql = 'SELECT * FROM nazwa_tabeli WHERE nazwa_kolumny = ?';
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $word);
$stmt->execute();
$res = $stmt->get_result();
$stmt->close();
connect($conn);
if ($res->num_rows) {
return $res->fetch_all(MYSQLI_ASSOC);
} else {
return null;
}
}
?>