· 9 years ago · Jun 21, 2017, 11:34 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 <p>Введите ваши даные</p><br><br>
23 <form action="" method="post">
24 <label>имÑ</label><br>
25 <input type="text" name="name" value="{$this->name}"> <br><br>
26 <label>ел почта</label><br>
27 <input type="text" name="email" value="{$this->email}"> <br><br>
28 <label>пароль</label><br>
29 <input type="password" name="password" value="{$this->password}"><br><br>
30 <input type="submit" value="Отправить">
31HTML;
32 return $form;
33 }
34
35 public function test() {
36 if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["password"])) {
37 $this->name = $_POST["name"];
38 $this->email = $_POST["email"];
39 $this->password = $_POST["password"];
40 $item = array("name" => $this->name, "email" => $this->email, "password" => $this->password);
41 echo$this->write($item);
42 }
43 }
44
45 public function connectDB() {
46 $link = mysql_connect($this->host, $this->username, $this->password);
47 if (!link) {
48 die("Ошибка подключениÑ:" . mysql_error() );
49 }
50 mysql_select_db($this->db) or die("Ðе могу найти БД." . mysql_error());
51 $this->buildDB();
52 return $link;
53 }
54
55
56 public function buildDB() {
57 $sql = 'CREATE TABLE IF NOT EXISTS `registration` (
58 `mid` int(11) NOT NULL AUTO_INCREMENT,
59 `name` varchar(20) NOT NULL,
60 `email` varchar(30) NOT NULL,
61 `password` varchar(20) NOT NULL,
62 PRIMARY KEY (`mid`)
63) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ';
64 $result = mysql_query($sql);
65 echo $result;
66 }
67
68 public function write($p) {
69 $sql = 'INSERT INTO registration(name, email, password) VALUES("'.$p["name"].'", "'.$p["email"].'", "'.$p["password"].'")';
70 return mysql_query($sql) or die(mysql_error());
71 }
72
73 public function display_public() {
74 $table = '<table>
75 <tr>
76 <td>â„–</td>
77 <td>Ваше имÑ</td>
78 <td>Ваша ел почта</td>
79 <td>Ваш пароль</td>
80 </tr>
81 ';
82 $sql = 'SELECT * FROM registration';
83 $result = mysql_query($sql);
84 while($row = mysql_fetch_array($result)) {
85 $table .= '<tr class="post">
86 <td class="mid">'.$row['mid'].'</td>
87 <td>'.$row["name"].'</td>
88 <td>'.$row['email'].'</td>
89 <td>'.$row['password'].'</td>
90 </tr>';
91 }
92 $table .= '</table>';
93 return $table;
94 }
95
96}
97$obj = new work_DB;
98$db_connection = $obj->connectDB();
99echo $obj->form();
100echo $obj->test();
101echo $obj->display_public();
102mysql_close($db_connection);
103?>