Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(0);
- class database
- {
- private $_connection = NULL;
- public function connect()
- {
- try
- {
- $this->_connection = new PDO('mysql:host=localhost; dbname=zadanie1; charset=utf8', 'root', '');
- }
- catch(PDOException $e)
- {
- echo "Błąd połączenia";
- }
- }
- public function query($q)
- {
- if(stristr($q, 'SELECT'))
- {
- $res = $this->_connection->query($q);
- $res->execute();
- $r = new resource();
- return $r->fetch($res);
- }
- if(stristr($q, 'DELETE'))
- {
- $delete = $this->_connection->query($q);
- $delete->execute();
- }
- if(stristr($q, 'UPDATE'))
- {
- $update = $this->_connection->query($q);
- $update->execute();
- }
- if(stristr($q, 'INSERT'))
- {
- $ins = $this->_connection->query($q);
- $ins->execute();
- }
- }
- }
- class resource
- {
- private $_resource = NULL;
- public function res($r)
- {
- $this->_resource = $r;
- }
- public function fetch($f)
- {
- return $f->fetch(PDO::FETCH_ASSOC);
- }
- }
- function table()
- {
- $db = new database();
- $db->connect();
- $c = $db->query('SELECT COUNT(Lp) AS Lp FROM Dane');
- $count = $c['Lp'];
- for($i = 1; $i <= $count; $i++)
- {
- $row = $db->query("SELECT * FROM Dane WHERE Lp = $i");
- echo '<tr><td>' .$row['Lp']. '</td><td>' . $row['Name']. '</td><td>' . $row['Surname']. '</td><td>' . $row['Date']. '</td><td>' . $row['Number']. '</td><td>' . $row['Adres'].'</td></tr>';
- }
- }
- function updateLp($rem)
- {
- $db = new database();
- $db->connect();
- $c = $db->query('SELECT COUNT(Lp) AS Lp FROM Dane');
- $count = $c['Lp'];
- if($db->query("SELECT Lp FROM Dane WHERE Lp = $rem+1") != "")
- {
- for($i = $rem+1; $i <= $count; $i++)
- {
- $db->query("UPDATE Dane SET Lp = Lp-1 WHERE Lp = $i");
- }
- }
- }
- function remove()
- {
- $remove = $_GET['rem'];
- $db = new database();
- $db->connect();
- if($db->query("SELECT Lp FROM Dane WHERE Lp = $remove") != "")
- {
- $db->query("DELETE FROM Dane WHERE Lp = $remove");
- updateLp($remove);
- }
- }
- function insert()
- {
- $db = new database();
- $db->connect();
- $name = $_GET["name"];
- $db->query("INSERT INTO Dane (Name) VALUES ('$name')");
- $_GET["name"] = "";
- }
- function prepare()
- {
- if($_GET["rem"] != "")
- {
- remove();
- }
- if($_GET["name"] != "")
- {
- insert();
- $_GET["name"] = "";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>Zadanie 1</title>
- <style>
- body
- {
- margin: 0;
- padding: 0;
- background: lightgrey;
- }
- td
- {
- text-align: center;
- width: 200px;
- height: 20px;
- }
- th
- {
- text-align: center;
- width: 200px;
- height: 20px;
- }
- .divtable
- {
- width: 1200px;
- min-height: 200px;
- margin-left: auto;
- margin-right: auto;
- margin-top: 10px;
- }
- .divremove
- {
- width: 50%;
- height: 50%;
- text-align: center;
- float: left;
- margin-top: 100px;
- }
- .divadd
- {
- width: 50%;
- height: 100%;
- text-align: center;
- float: right;
- }
- form
- {
- width: 400px;
- height: 50%;
- border-style: solid;
- border-color: grey;
- margin-left: auto;
- margin-right: auto;
- border-radius: 20px 20px 20px 20px;
- padding: 5px 5px 100px 5px;
- }
- p
- {
- width: 300px;
- height: 40px;
- margin-left: auto;
- margin-right: auto;
- font-size: 20px;
- }
- input
- {
- float: right;
- height: 35px;
- border-radius: 20px 20px 20px 20px;
- border-style: none;
- }
- .button
- {
- width: 100px;
- height: 50px;
- margin-right: 150px;
- margin-top: 20px;
- font-size: 24px;
- border-radius: 20px 20px 20px 20px;
- }
- </style>
- </head>
- <body>
- <?php prepare() ?>
- <div class="divtable">
- <table>
- <tr>
- <th>Lp</th>
- <th>Imię</th>
- <th>Nazwisko</th>
- <th>Data urodzenia</th>
- <th>Numer telefonu</th>
- <th>Adres zamieszkania</th>
- </tr>
- <?php table(); ?>
- </table>
- </div>
- <div class="divremove">
- <form metod ="get" action="index.php">
- <h1>Usuwanie z tabeli</h1>
- <p>Nr Lp. <input type="number" name="rem"/></p>
- <input type="submit" value="Usuń" name="usun" class="button"/>
- </form>
- </div>
- <div class="divadd">
- <form metod="get" action="index.php">
- <h1>Dodawanie do tabali</h1>
- <p>Imię: <input type="text" name="name" /></p>
- <p>Nazwisko <input type="text" name="surname" /></p>
- <p>Data urodzenia <input type="date" name="date"/></p>
- <p>Numer telefonu <input type="number" name="number"/></p>
- <p style="width:350px;">Adres zamieszkania <input type="text" name="adres" /></p>
- <input type="submit" value="Dodaj" name="add" class ="button" />
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement