learning, task4/6
4 -> not working 5,6 -> not done yet
This commit is contained in:
49
lernen/ka-1-lbt3+4/aufgabe2.php
Normal file
49
lernen/ka-1-lbt3+4/aufgabe2.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=localhost;dbname=lernenjahr2","root","deinPasswort");
|
||||
|
||||
$produkteTabelle = 'CREATE TABLE IF NOT EXISTS produkte (
|
||||
id INTEGER AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255),
|
||||
preis INTEGER,
|
||||
lagerbestand INTEGER)';
|
||||
|
||||
$produkteBestellung = 'CREATE TABLE IF NOT EXISTS bestellungen (
|
||||
id INTEGER AUTO_INCREMENT PRIMARY KEY,
|
||||
produkt_id INTEGER,
|
||||
anzahl INTEGER,
|
||||
datum DATE)';
|
||||
|
||||
$createTable = $pdo->exec($produkteTabelle);
|
||||
echo "TABELLE produkte ERSTELLT <br>";
|
||||
$createTable = $pdo->exec($produkteBestellung);
|
||||
echo "TABELLE Bestellung ERSTELLT <br>";
|
||||
|
||||
$lagerbestandProProduktQuery = 'SELECT `name`, lagerbestand, id FROM produkte';
|
||||
$lagerbestandProProdukt = $pdo->query($lagerbestandProProduktQuery)->fetchAll();
|
||||
|
||||
echo "<pre>";
|
||||
print_r($lagerbestandProProdukt);
|
||||
echo "</pre>";
|
||||
|
||||
foreach($lagerbestandProProdukt as $row){
|
||||
if ($row[1] >= 1){
|
||||
echo "Von '$row[0]' haben wir aktuell $row[1] (ID: $row[2]). <br>";
|
||||
} else {
|
||||
echo "Von '$row[0]' haben wir nichts mehr <br>";
|
||||
$createBestellung = $pdo->exec("INSERT INTO bestellungen VALUES(
|
||||
null,
|
||||
$row[2],
|
||||
999,
|
||||
'2025-01-12'
|
||||
)");
|
||||
echo "Von '$row[0]' wurden 999 stück bestellt.";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "Error: ", $e->getMessage();
|
||||
}
|
||||
Reference in New Issue
Block a user