· 9 years ago · Jun 25, 2017, 11:44 AM
1<?php
2class work_DB {
3 public $host = 'localhost';
4 public $username = 'root';
5 public $password = '';
6 public $db = 'DB';
7
8 public function getName() {
9 return $this->name;
10 }
11
12 public function getEmail() {
13 return $this->email;
14 }
15
16 public function getPassword() {
17 return $this->password;
18 }
19
20 public function form() {
21 $form = <<<HTML
22 <head>
23 <link rel="stylesheet" href="CSS/work_DB.css"/>
24 <link href="https://fonts.googleapis.com/css?family=Cormorant|Cormorant+Infant|Open+Sans+Condensed:300" rel="stylesheet"></head>
25 <form action="" method="post">
26 <div class="form">
27 <div class="form-text">
28 <h1>Введите ваши даные</h1>
29 <p>Заполните Ð¿Ð¾Ð»Ñ Ð´Ð»Ñ Ð²Ð°ÑˆÐµÐ³Ð¾ беззопаÑного доÑтупа на Ñайт</p>
30 </div>
31 <div class="input">
32 <input class="place" type="text" name="name" placeholder="ИмÑ" value="{$this->name}"> <br>
33 <input type="text" name="email" placeholder="Ел почта" value="{$this->email}"> <br>
34 <input type="password" name="password" placeholder="Пароль" value="{$this->password}"><br>
35 <input class="button" type="submit" value="Отправить"><br>
36 </form>
37 </div>
38 </div><br><br><br>
39HTML;
40 return $form;
41 }
42
43 public function test() {
44 if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["password"])) {
45 $this->name = $_POST["name"];
46 $this->email = $_POST["email"];
47 $this->password = $_POST["password"];
48 $item = array("name" => $this->name, "email" => $this->email, "password" => $this->password);
49 $this->write($item);
50 }
51 }
52
53 public function connectDB() {
54 $link = mysql_connect($this->host, $this->username, $this->password);
55 if (!link) {
56 die("Ошибка подключениÑ:" . mysql_error() );
57 }
58 mysql_select_db($this->db) or die("Ðе могу найти БД." . mysql_error());
59 $this->buildDB();
60 return $link;
61 }
62
63
64 public function buildDB() {
65 $sql = 'CREATE TABLE IF NOT EXISTS `registration` (
66 `mid` int(11) NOT NULL AUTO_INCREMENT,
67 `name` varchar(20) NOT NULL,
68 `email` varchar(30) NOT NULL,
69 `password` varchar(20) NOT NULL,
70 PRIMARY KEY (`mid`)
71) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ';
72 $result = mysql_query($sql);
73 return $result;
74 }
75
76 public function write($p) {
77 $sql = 'INSERT INTO registration(name, email, password) VALUES("'.$p["name"].'", "'.$p["email"].'", "'.$p["password"].'")';
78 return mysql_query($sql) or die(mysql_error());
79 }
80
81 public function display_public() {
82 $table = '<table>
83 <tr>
84 <td>â„–</td>
85 <td>Ваше имÑ</td>
86 <td>Ваша ел почта</td>
87 <td>Ваш пароль</td>
88 </tr>
89 ';
90 $sql = 'SELECT * FROM registration';
91 $result = mysql_query($sql);
92 while($row = mysql_fetch_array($result)) {
93 $table .= '<tr class="post">
94 <td>'.$row['mid'].'</td>
95 <td>'.$row["name"].'</td>
96 <td>'.$row['email'].'</td>
97 <td>'.$row['password'].'</td>
98 </tr>';
99 }
100 $table .= '</table>';
101 return $table;
102 }
103
104}
105$obj = new work_DB;
106$db_connection = $obj->connectDB();
107echo $obj->form();
108echo $obj->test();
109echo $obj->display_public();
110mysql_close($db_connection);
111?>