Guest User

Untitled

a guest
Jun 20th, 2017
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. <?php
  2. class work_DB {
  3.      public $host = 'localhost';
  4.     public $username = 'root';
  5.     public $password = '';
  6.     public $db = "mydb";
  7.    
  8.     public function getName() {
  9.         return $this->name;
  10.     }
  11.     public function getEmail() {
  12.         return $this->email;
  13.     }
  14.      
  15.      public function getPassword() {
  16.          return $this->password;
  17.      }
  18.      
  19.     public function form() {
  20.         $form = <<<HTML
  21. <form action=" " method="post">
  22. <p>Введите ваши даные</p><br><br>
  23. <label>Имя</label><br>
  24. <input type="text" name="name" value="{$this->name}"><br><br>
  25. <label>Ел почта</label><br>
  26. <input type="text" name="email" value="{$this->email}"><br><br>
  27. <label>Пароль</label><br>
  28. <input type="password" name="password" value="{$this->password}"><br><br>
  29. <input type="submit" value="Отправить">
  30. </form>
  31. HTML;
  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.             $kotya = array("name" => "$this->name", "email" => "$this->email", "password" => "$this->password");
  41.             $this->write($kotya);
  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.     public function buildDB() {
  56.         $sql = ' CREATE TABLE IF NOT EXISTS `registration` (
  57.  `mid` int(11) NOT NULL AUTO_INCREMENT,
  58.  `name` varchar(20) DEFAULT NULL,
  59.  `email` varchar(30) DEFAULT NULL,
  60.  `password` varchar(20) DEFAULT NULL,
  61.  PRIMARY KEY (`mid`)
  62. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ';
  63.         $result = mysql_query($sql);
  64.         echo $result;
  65.          
  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);
  71.      }
  72.    
  73.  
  74.  
  75.      public function display_public() {
  76.          $sql = 'SELECT * FROM registration';
  77.          $result = mysql_query($sql) or die (mysql_error());
  78.         echo $table = "<table>
  79.          <tr>
  80.          <td>номер</td>
  81.          <td>имя</td>
  82.          <td>ел почта</td>
  83.          <td>пароль</td>
  84.          </tr>";
  85.          while($row = mysql_fetch_array($result)) {
  86.             $table .= '<tr class="post">
  87.               <td>' . $row['mid'] . ' </td>
  88.              <td> '. $row['name'].' </td>
  89.              <td>' . $row['email'] . '</td>
  90.           <td> '. $row['password'] .'</td>
  91.              </tr>';
  92.          }
  93.          $table .= "</table>";
  94.          return $table;
  95.      }
  96.    
  97. }
  98. $obj1 = new work_DB;
  99. $db_connection = $obj1->connectDB();
  100. echo $obj1->form();
  101. echo $obj1-> test();
  102. echo $obj1->display_public();
  103. mysql_close($db_connection);
  104. ?>
Add Comment
Please, Sign In to add comment