PHP: MySQL Database Support in PHP using the Shop database as an example

Below is the code for the application which is used in the training material for the MySQL database.

sklep.php
<?php
// Ustawienia połączenia z bazą danych
$host = 'ADRES IP';
$user = 'LOGIN';
$password = 'HASŁO';
$database = 'sklep';
 
// Połączenie z bazą danych
$conn = new mysqli($host, $user, $password, $database);
 
// Sprawdzenie połączenia
if ($conn->connect_error) {
    die("Connection error: " . $conn->connect_error);
}
 
// Variable to store results and errors
$resultMessage = '';
$tableOutput = '';
 
// Form handling
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $query = $_POST['sql_query'] ?? '';
 
    if (!empty($query)) {
        if ($result = $conn->query($query)) {
            if ($result instanceof mysqli_result) {
                // Create a table with the results
                $tableOutput .= "<table border='1' cellpadding='5'><tr>";
                while ($fieldinfo = $result->fetch_field()) {
                    $tableOutput .= "<th>{$fieldinfo->name}</th>";
                }
                $tableOutput .= "</tr>";
 
                while ($row = $result->fetch_assoc()) {
                    $tableOutput .= "<tr>";
                    foreach ($row as $cell) {
                        // Safe value processing (null -> empty string)
                        $tableOutput .= "<td>" . htmlspecialchars($cell ?? '') . "</td>";
                    }
                    $tableOutput .= "</tr>";
                }
                $tableOutput .= "</table>";
 
                $result->free();
            } else {
                $resultMessage = "Request executed successfully."
            }
        } else {
            $resultMessage = "Query error: " . $conn->error;
        }
    } else {
        $resultMessage = "Query field cannot be empty."
    }
}
 
$conn->close();
?>
 
<!DOCTYPE html>
<html>
<head>
    <title>Shop base preview</title>
</head>
<body>
    <h1>Base preview Shop</h1>
    <h4>Only SELECT queries can be performed here</h4>
    <form method="post">
        <label for="sql_query">Enter your SQL query:</label><br>
        <textarea name="sql_query" id="sql_query" rows="6" cols="80"><?= htmlspecialchars($_POST['sql_query'] ?? '') ?></textarea><br><br>
        <input type="submit" value="Wykonaj zapytanie">
    </form>
 
    <div style="margin-top:20px;">
        <?php if (!empty($resultMessage)) echo "<p><strong>$resultMessage</strong></p>"; ?>
        <?= $tableOutput ?>
    </div>
	<a href="LINK">Kod tej aplikacji jest dostępny tutaj</a>
</body>
</html>