DOCTYPE html php class UserInfoPDO extends PDO конструктор унаследован

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<?php
class UserInfoPDO extends PDO
{
//конструктор унаследованный от PDO и немного инфы добавлено
public function __construct($servername, $dbname, $username, $password) {
try {
parent::__construct("mysql:host=$servername;dbname=$dbname;charset=cp1251", $username, $password);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function __destruct() {
$connection = null;
}
// генерируем пароль с проверкой на наличие существующего пароля
public function GeneratePassword($email){
$check = "SELECT password FROM ListOfUsers WHERE email='$email'";
$empty = $this->prepare($check);
$empty->execute();
$result = $empty->setFetchMode(PDO::FETCH_ASSOC);
$empty = $empty->fetchAll();
if ($empty[0]["password"] !== ""){
echo "Password was already generated!";
}
else {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$password = substr(str_shuffle($alphabet),0, 10);
$encriptedPass = md5($password);
$sql = "UPDATE ListOfUsers SET password='$encriptedPass' WHERE email='$email'";
$this->exec($sql);
}
}
//берем инфу из csv файла
public function AddInfoFromCSV($filename){
$myfile = fopen("$filename", 'r') or die ("Unable to open file!");
$counter = 0;
while (feof($myfile) !== true){
$userData[$counter] = fgets($myfile);
$userData[$counter] = trim($userData[$counter]);
$userData[$counter] = explode(";", $userData[$counter]);
$counter++;
}
foreach ($userData as $key => $value) {
$date = date("d-m-Y", strtotime($value[2]));
$sqlQuery = "INSERT INTO ListOfUsers (name, email, registration)
VALUES ('$value[0]', '$value[1]', '$date')";
$this->exec($sqlQuery);
}
}
//создаем таблицу в подготовленной заранее базе данных
//длинна пароля 32 символа - максимальная длинная хэша
public function CreateTable(){
$sql = "CREATE TABLE ListOfUsers (
name VARCHAR(50) NOT NULL ,
email VARCHAR(30) NOT NULL PRIMARY KEY,
registration VARCHAR(10) NOT NULL,
password VARCHAR(32) NOT NULL)
CHARACTER SET cp1251";
$this->exec($sql);
}
//вывод информации из базы данных на веб форму
public function GenerateOutput(){
$sql = "SELECT * FROM ListOfUsers";
$allData = $this->prepare($sql);
$allData->execute();
$mode = $allData->setFetchMode(PDO::FETCH_ASSOC);
$allData = $allData->fetchAll();
$output = "<table>";
foreach ($allData as $key => $value) {
$output = $output."<tr>";
foreach ($value as $key1 => $value1) {
$output = $output."<td>".$value1."</td>";
}
$output = $output."</tr>";
}
$output = $output."</table>";
echo $output;
}
}
?>
<html>
<head>
<meta charset="cp1251">
<title></title>
<style>
table, tr, td {border: 1px solid black;
border-collapse: collapse;
padding: 5px;}
</style>
</head>
<body>
<?php
//параметры нашей базы данных(для подключения)
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "users";
$test = new UserInfoPDO($servername, $dbname, $username, $password);
$test->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$test->CreateTable();
$test->AddInfoFromCSV("CSV.csv");
$test->GeneratePassword("shutovcorp@mail.ru");
$test->GeneratePassword("polinovfighter@google.ru");
$test->GeneratePassword("koldanst@hotmail.com");
$test->GenerateOutput();
?>
</body>
</html>