Я пытаюсь улучшить соединение с БД через классы php. Я нашел несколько хороших решений здесь и внедрил их в мой код. Мне нужна помощь, чтобы войти:
JavaScript / JQuery:
userService = new UserService();
userService.login(user).done(function (data) {
if (data) {
console.log('yess');
window.location.href = "./AdminPanel.html";
} else {
console.log('wroooooong')
}
}
user.service.js
function UserService() {}
UserService.prototype.isAdmin = function(currentUser) {
var dataService = {};
dataService['reason'] = "isAdmin";
dataService['user'] = currentUser;
return $.ajax({
type: "POST",
data: {
loginData : (currentUser)
},
url: "php/controller/UserController.php",
dataType: "json",
success: function(data) {
return true;
},
error: function (data) {
return false;
}
});
};
UserService.prototype.login = function(user) {
var dataService = {};
dataService['reason'] = "login";
dataService['user'] = user;
return $.ajax({
type: "POST",
data: {dataService : dataService},
url: "./php/BackendHandler.php",
async: false,
dataType: "json",
success: function(data) {
console.log(data);
return data;
},
error: function (data) {
console.log(data);
return data;
}
});
};
UserService.prototype.loginAdmin = function() {
};
UserService.prototype.register = function(user) {
};
UserService.prototype.destroy = function(user) {
};
выше Я использую функцию обратного вызова (возврат) с ajax / jquery
BackendHandler.php
<?php
require "./controller/UserController.php";
$dataService = $_POST['dataService'];
switch ($dataService['reason']) {
case "login":
$login = new UserController();
$login->login($dataService['user']);
break;
default:
echo false;
break;
}
UserController.php
<?php
$path = "./services/UserService.php";
require "$path";
require('./Connect.php');
class UserController
{
private $connection;
private $username;
private $password;
function __construct()
{
$this->connection = new Connect();
}
public function login($userdata)
{
$this->username = $userdata['username'];
$this->password = $userdata['password'];
$this->password = md5(mysqli_real_escape_string($this->connection->connect(), $userdata['password']));
$abfrage = "SELECT username, password FROM user WHERE username='$this->username' AND password='$this->password' LIMIT 1";
$getLogin = mysqli_query($this->connection->connect(), $abfrage);
$row = mysqli_num_rows($getLogin);
while ($row == 1) {
$firma = $row["companyID"];
}
echo $firma;
$this->connection->disconnect();
}
private function register()
{
$this->connection->disconnect();
}
private function destroy()
{
$this->connection->disconnect();
}
private function getAllUsers()
{
$this->connection->disconnect();
}
}
connect.php
include "DbConnection.php";
class Connect
{
protected $connectionToDatabase = false;
protected $charset = "utf8";
function __construct() {
$this -> connectionToDatabase = new DbConnection();
}
public function connect(){
return $this->connectionToDatabase ->dbConnect();
}
public function disconnect(){
return $this->connectionToDatabase -> dbDisconnect();
}
}
DbConnection.php
<?php
include_once('Dbconfig.php');
class DbConnection extends Dbconfig {
public $connectionLink;
public $dataSet;
private $sqlQuery;
protected $databaseName;
protected $hostName;
protected $userName;
protected $passCode;
function __construct() {
$this -> connectionLink = NULL;
$this -> sqlQuery = NULL;
$this -> dataSet = NULL;
$dbPara = new Dbconfig();
$this -> databaseName = $dbPara -> dbName;
$this -> hostName = $dbPara -> serverName;
$this -> userName = $dbPara -> userName;
$this -> passCode = $dbPara ->passCode;
// $this->dbConnect();
}
function dbConnect() {
$this -> connectionLink = mysqli_connect($this -> hostName, $this -> userName,$this -> passCode);
mysqli_select_db( $this -> connectionLink, $this -> databaseName);
return $this -> connectionLink;
}
function dbDisconnect() {
$this -> connectionLink = NULL;
$this -> sqlQuery = NULL;
$this -> dataSet = NULL;
$this -> databaseName = NULL;
$this -> hostName = NULL;
$this -> userName = NULL;
$this -> passCode = NULL;
}
}
Dbconfig.php
<?php
class Dbconfig {
protected $serverName;
protected $userName;
protected $passCode;
protected $dbName;
function __construct() {
$this -> serverName = 'myServer';
$this -> userName = 'stack';
$this -> passCode = 'overflow';
$this -> dbName = 'myDB';
}
}
Не могли бы вы выполнить недостающие или не очень хорошо запрограммированные части. Я немного запутался со всем факторингом.