You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
<?php
|
|
|
|
// connect to database sqlite database using PDO
|
|
$pdo = new PDO('sqlite:db.sqlite');
|
|
|
|
// C = CREATE
|
|
// insert data into table
|
|
$query = $pdo->prepare(
|
|
'INSERT INTO allergies ("label", "from", "to") VALUES (:label, :from, :to)');
|
|
$query->execute([
|
|
'label' => 'Peanuts',
|
|
'from' => strtotime('2025-01-01'),
|
|
'to' => strtotime('2025-12-31'),
|
|
]);
|
|
|
|
// last inserted id
|
|
$lastId = $pdo->lastInsertId();
|
|
echo 'Last inserted id: ' . $lastId;
|
|
|
|
// R = READ
|
|
// select data from table
|
|
$query = $pdo->prepare('SELECT * FROM allergies');
|
|
$query->execute();
|
|
$allergies = $query->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo "<table border='1'>";
|
|
foreach ($allergies as $allergy) {
|
|
// echo '<pre>';
|
|
// print_r($allergy);
|
|
// echo '</pre>';
|
|
echo "<tr>";
|
|
echo "<td>" . $allergy['id'] . "</td>";
|
|
echo "<td>" . $allergy['label'] . "</td>";
|
|
echo "<td>" . date('d.m. Y', $allergy['from']) . "</td>";
|
|
echo "<td>" . date('d.m. Y', $allergy['to']) . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
echo "</table>";
|
|
|
|
|
|
// U = UPDATE
|
|
// update data in table
|
|
$query = $pdo->prepare('UPDATE allergies SET "label" = :label WHERE "id" = :id');
|
|
$query->execute([
|
|
'label' => 'Peanuts (updated)',
|
|
'id' => $lastId,
|
|
]);
|
|
|
|
|
|
// D = DELETE
|
|
// delete data from table
|
|
$query = $pdo->prepare('DELETE FROM allergies WHERE "id" = :id');
|
|
$query->execute([
|
|
'id' => $lastId,
|
|
]);
|
|
|