Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class work_DB {
- public $host = 'localhost';
- public $username = 'root';
- public $password = '';
- public $db = 'DB';
- public function getName() {
- return $this->name;
- }
- public function getEmail() {
- return $this->email;
- }
- public function getPassword() {
- return $this->password;
- }
- public function form() {
- $form = <<<HTML
- <p>Введите ваши даные</p><br><br>
- <form action="" method="post">
- <label>имя</label><br>
- <input type="text" name="name" value="{$this->name}"> <br><br>
- <label>ел почта</label><br>
- <input type="text" name="email" value="{$this->email}"> <br><br>
- <label>пароль</label><br>
- <input type="password" name="password" value="{$this->password}"><br><br>
- <input type="submit" value="Отправить">
- HTML;
- return $form;
- }
- public function test() {
- if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["password"])) {
- $this->name = $_POST["name"];
- $this->email = $_POST["email"];
- $this->password = $_POST["password"];
- $item = array("name" => $this->name, "email" => $this->email, "password" => $this->password);
- echo$this->write($item);
- }
- }
- public function connectDB() {
- $link = mysql_connect($this->host, $this->username, $this->password);
- if (!link) {
- die("Ошибка подключения:" . mysql_error() );
- }
- mysql_select_db($this->db) or die("Не могу найти БД." . mysql_error());
- $this->buildDB();
- return $link;
- }
- public function buildDB() {
- $sql = 'CREATE TABLE IF NOT EXISTS `registration` (
- `mid` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(20) NOT NULL,
- `email` varchar(30) NOT NULL,
- `password` varchar(20) NOT NULL,
- PRIMARY KEY (`mid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ';
- $result = mysql_query($sql);
- echo $result;
- }
- public function write($p) {
- $sql = 'INSERT INTO registration(name, email, password) VALUES("'.$p["name"].'", "'.$p["email"].'", "'.$p["password"].'")';
- return mysql_query($sql) or die(mysql_error());
- }
- public function display_public() {
- $table = '<table>
- <tr>
- <td>№</td>
- <td>Ваше имя</td>
- <td>Ваша ел почта</td>
- <td>Ваш пароль</td>
- </tr>
- ';
- $sql = 'SELECT * FROM registration';
- $result = mysql_query($sql);
- while($row = mysql_fetch_array($result)) {
- $table .= '<tr class="post">
- <td class="mid">'.$row['mid'].'</td>
- <td>'.$row["name"].'</td>
- <td>'.$row['email'].'</td>
- <td>'.$row['password'].'</td>
- </tr>';
- }
- $table .= '</table>';
- return $table;
- }
- }
- $obj = new work_DB;
- $db_connection = $obj->connectDB();
- echo $obj->form();
- echo $obj->test();
- echo $obj->display_public();
- mysql_close($db_connection);
- ?>
Add Comment
Please, Sign In to add comment