Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
See the problem with building SQL from user input, and the one-line fix.
Never put user input straight into SQL:
// DANGEROUS
$sql = "SELECT * FROM users WHERE email = '" . $_GET['email'] . "'";If someone sends ' OR '1'='1, the query changes meaning and may return every user.
Send the SQL and the data separately. The database treats the data as plain values, never as code.
$st = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$st->execute([$_GET['email']]);
$user = $st->fetch();$st = $pdo->prepare('SELECT * FROM posts WHERE category = :cat AND published = :p');
$st->execute([':cat' => $cat, ':p' => 1]);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.