Mvh Joep
Constants.php:
Kode: Vælg alt
<?php
define('DB_NAME', 'roedel');
define('DB_USER', 'root');
define('DB_HOST', 'localhost');
define('DB_PASS', '');
?>
Kode: Vælg alt
<?php
class DBConnect {
private $con;
function __construct() {
}
function connect() {
require_once dirname(__FILE__).'/Constants.php';
try{
$this->con = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
return $this->con;
} catch (Exception $ex) {
echo "Er is geen verbinding. Probeer later opnieuw!" . $ex;
}
}
}
?>
Kode: Vælg alt
<?php
require_once dirname(__FILE__).'/DBConnect.php';
class DBOperations {
private $con;
function __construct() {
$db = new DBConnect();
$this->con = $db->connect();
}
public function createUser($name, $pass, $email){
$password = md5($pass);
$rank = "lid";
$stmt = $this->con->prepare("INSERT INTO 'users' ('id', 'name', 'password', 'email', 'rank') VALUES (NULL, ?, ?, ?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $password);
$stmt->bindParam(3, $email);
$stmt->bindParam(4, $rank);
return $stmt->execute();
echo "\nPDOStatement::errorInfo():\n";
$arr = $stmt->errorInfo();
print_r($arr);
$this->con = null;
}
}
?>
Kode: Vælg alt
<?php
require_once '../Includes/DBOperations.php';
$response = array();
$db = new DBOperations();
if ($db->createUser($_POST['name'], $_POST['password'], $_POST['email'])){
$response['error'] = false;
$response['message'] = "Je bent nu geregistreert!";
} else {
$response['error'] = true;
$response['message'] = "Er ging iets mis. Probeer opnieuw!";
}
echo json_encode($response);
?>