Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
How data travels from a form to PHP, which method to use, and how to print it back without opening a security hole.
search.php?q=php). Use it for searching and filtering, because the page can be bookmarked and shared.<form method="post" action="hello.php">
<label for="name">Your name</label>
<input id="name" name="name" required maxlength="80">
<button>Send</button>
</form><?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim((string)($_POST['name'] ?? ''));
if ($name === '') {
$error = 'Please type your name.';
} else {
echo 'Hello, ' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
}
}Three habits are worth building from day one:
1. Use ?? so a missing field does not cause a warning.
2. Trim and check every value on the server. Browser checks such as required can be bypassed.
3. Escape when printing with htmlspecialchars(). If a visitor types <script>, it is shown as text instead of running (this stops XSS).
echo $_GET['q']; // prints raw input: unsafe
$sql = "SELECT * FROM t WHERE a='$x'"; // builds SQL from input: unsafeFor databases, use prepared statements (see the tutorial on prepared statements).
If the page just saved something, redirect to another page so a browser refresh does not send the form twice:
header('Location: thanks.php', true, 303);
exit;Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.