· 6 years ago · Jun 07, 2019, 10:22 AM
1<html>
2<head>
3 <title>Jakub Wojton</title>
4</head>
5<body>
6<table>
7 <tr>
8 <td>ID</td>
9 <td>Imie</td>
10 <td>Nazwisko</td>
11 </tr>
12
13 <?php
14 $connection = new PDO("mysql:host=127.0.0.1;dbname=temp", "root", "");
15 $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
16 $statement = $connection->prepare("SELECT * FROM `jakub_wojton`");
17 $statement->execute();
18 foreach ($statement->fetchAll() as $item) {
19 echo("<tr>");
20 echo("<td>" . $item["id"] . "</td>");
21 echo("<td>" . $item["name"] . "</td>");
22 echo("<td>" . $item["surname"] . "</td>");
23 echo("</tr>");
24 }
25 $statement->closeCursor();
26 $connection = null;
27 ?>
28</table>
29
30<br /><br />
31<h1>Dodaj nowy rekord</h1>
32<form action="/index.php" method="post">
33 Imie: <input type="text" name="name" required/> <br />
34 Naziwsko: <input type="text" name="surname" required/> <br />
35 <input type="submit" value="Zapisz" />
36</form>
37
38<br /><br />
39<h1>Edytuj rekord</h1>
40<form action="/index.php" method="post">
41 ID: <input type="number" name="id" required/> <br />
42 Imie: <input type="text" name="name" required/> <br />
43 Naziwsko: <input type="text" name="surname" required/> <br />
44 <input type="submit" value="Zapisz" />
45</form>
46
47</body>
48</html>
49
50<?php
51// CREATE TABLE IF NOT EXISTS `jakub_wojton` (`id` int(11) NOT NULL PRIMARY AUTO_INCREMENT, `name` text NOT NULL, `surname` text NOT NULL)
52$connection = new PDO("mysql:host=127.0.0.1;dbname=temp", "root", "");
53$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
54
55if (!isset($_REQUEST["id"]) && isset($_REQUEST["name"]) && isset($_REQUEST["surname"])) {
56 $name = $_REQUEST["name"];
57 $surname = $_REQUEST["surname"];
58
59 $statement = $connection->prepare("INSERT INTO `jakub_wojton` VALUES (NULL, :name, :surname)");
60 $statement->bindParam(":name", $name);
61 $statement->bindParam(":surname", $surname);
62 $statement->execute();
63 $statement->closeCursor();
64
65 echo("Rekord zostal dodany!");
66}
67
68if (isset($_REQUEST["id"]) && isset($_REQUEST["name"]) && isset($_REQUEST["surname"])) {
69 $id = $_REQUEST["id"];
70 $name = $_REQUEST["name"];
71 $surname = $_REQUEST["surname"];
72
73 $statement = $connection->prepare("UPDATE `jakub_wojton` SET `name` = :name, `surname` = :surname WHERE `id` = :id");
74 $statement->bindParam(":id", $id);
75 $statement->bindParam(":name", $name);
76 $statement->bindParam(":surname", $surname);
77 $statement->execute();
78 $statement->closeCursor();
79
80 echo("Rekord zostal zapisany!");
81}
82
83$connection = null;