2 von 16

Grundlagen · Kapitel 2 von 16

PHP · MySQL · HTML/CSS – Schritt für Schritt

Kapitel 1

Datenbank-Schema

Fünf Tabellen: users, categories, products, orders, order_items. Skizze zuerst auf Papier – Beziehungen: eine Kategorie hat viele Produkte (1:n), ein User hat viele Bestellungen (1:n), eine Bestellung hat viele Positionen (1:n), jede Position gehört zu einem Produkt (n:1).

Schema anlegen (in phpMyAdmin oder MySQL-Konsole)
CREATE DATABASE webshop_otto CHARACTER SET utf8mb4;
USE webshop_otto;

CREATE TABLE users (
    id            INT AUTO_INCREMENT PRIMARY KEY,
    username      VARCHAR(50)  NOT NULL UNIQUE,
    email         VARCHAR(100) NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    rolle         VARCHAR(20)  NOT NULL DEFAULT 'user',
    created_at    DATETIME     DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE categories (
    id   INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
) ENGINE=InnoDB;

CREATE TABLE products (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    artikelcode VARCHAR(20)   NOT NULL UNIQUE,
    name        VARCHAR(100)  NOT NULL,
    description TEXT,
    price       DECIMAL(10,2) NOT NULL,
    picture     VARCHAR(255),
    freigeben   TINYINT(1)    NOT NULL DEFAULT 0,
    category_id INT,
    FOREIGN KEY (category_id) REFERENCES categories(id)
) ENGINE=InnoDB;

CREATE TABLE orders (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    user_id     INT NOT NULL,
    bestellt_am DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB;

CREATE TABLE order_items (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    order_id   INT NOT NULL,
    product_id INT NOT NULL,
    menge      INT NOT NULL,
    preis      DECIMAL(10,2) NOT NULL,
    FOREIGN KEY (order_id)   REFERENCES orders(id),
    FOREIGN KEY (product_id) REFERENCES products(id)
) ENGINE=InnoDB;
Warum so?
DECIMAL(10,2) für Preise, nie FLOAT (Rundungsfehler bei Geld).
preis in order_items gespeichert – weil sich der Produktpreis später ändern kann, die alte Rechnung aber nicht.
artikelcode UNIQUE – so kann die DB selbst schon doppelte Codes verhindern.
InnoDB – nur InnoDB unterstützt Foreign Keys und Transaktionen (MyISAM nicht).
Achtung Foreign Key Direction: Der FK gehört auf die "n"-Seite (die "viele" Seite). Also category_id steht in products, nicht umgekehrt. Häufiger Anfängerfehler.
Erster Admin-User + Testdaten
-- passwort_hash für 'test1234' — bei dir mit password_hash() eigenen erzeugen
INSERT INTO users (username, email, password_hash, rolle)
VALUES ('admin', 'admin@shop.local',
        '$2y$10$exampleReplaceThisWithRealHash', 'admin');

INSERT INTO categories (name) VALUES ('Elektronik'), ('Bücher'), ('Sonstiges');

16 Kapitel · ein Projekt

Der Codeinander Lernpfad

DEEN in Vorbereitung

Grundlagen

01GrundlagenIntro & Setup 02GrundlagenDatenbank-Schema 03Grundlagendb.php – Verbindung

Auth

04Authregister.php 05Authlogin.php 06Authlogout.php

Shop

07Shopnavbar.php 08Shopindex.php – Liste 09Shopproductdetail.php

Warenkorb

10Warenkorbcart_add.php 11Warenkorbwarenkorb.php 12Warenkorbcart_remove.php

Admin

13Adminstatistiken.php + Admin 14Admincheckout.php + Rechnung

Referenz

15ReferenzSQL-Cheatsheet 16Referenzzertifizierung-Checkliste