So i have made an SignUP form in AngularJS+PHP+MySQL and now i want to catch PDO exception in my angular so I can make an IF duplicate entry for example 'login' I can print it out in my Angular, but i have no idea where to begin. I have googled a bit but can't find anything really helpful.
This is my .controller
:
.controller('registerController', function($scope, $http) {
$scope.registerData = {firstname : null, lastname : null, login: null, password : null, email : null, city : null, postalcode : null, adress: null, country: null};
$scope.registerFunction = function() {
$http({
method: "post",
url: './php/registration.php',
data: {
firstname: $scope.registerData.firstname,
lastname: $scope.registerData.lastname,
login: $scope.registerData.login,
password: $scope.registerData.password,
email: $scope.registerData.email,
city: $scope.registerData.city,
postalcode: $scope.registerData.postalcode,
adress: $scope.registerData.adress,
country: $scope.registerData.country,
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
swal("Good job!", "You have been registered!", "success");
};
})
This is my form
in html+bootstrap
:
<div class="modal fade" id="registerModal">
<div class="modal-dialog">
<div class="modal-content" ng-controller="registerController">
<div class="modal-header"><h4 class="modal-title">Sign Up</h4></br><button type="button" class="close" data-dismiss="modal">×</button></div>
<div class="modal-body"><form>
<div class="row">
<div class="col-lg-6">
<label style="float: left;"><b>Firstname:</b></label>
<input type="text" ng-model="registerData.firstname" class="form-control">
<label style="float: left;"><b>Lastname:</b></label>
<input type="text" ng-model="registerData.lastname" class="form-control">
<label style="float: left;"><b><span class="redstar">*</span> Login:</b></label>
<input type="text" ng-model="registerData.login" class="form-control">
<label style="float: left;"><b><span class="redstar">*</span> Password:</b></label>
<input type="password" ng-model="registerData.password" class="form-control">
<label style="float: left;"><b><span class="redstar">*</span> Repeat Password:</b></label>
<input type="password" class="form-control">
</div>
<div class="col-lg-6">
<label style="float: left;"><b><span class="redstar">*</span> E-Mail:</b></label>
<input type="text" ng-model="registerData.email" class="form-control">
<label style="float: left;"><b>City:</b></label>
<input type="text" ng-model="registerData.city" class="form-control">
<label style="float: left;"><b>Postal Code:</b></label>
<input type="text" ng-model="registerData.postalcode" class="form-control">
<label style="float: left;"><b>Adress:</b></label>
<input type="text" ng-model="registerData.adress" class="form-control">
<label style="float: left;"><b>Country:</b></label>
<select class="form-control" ng-model="registerData.country" required>
<option ng-repeat="item in countries" value="{{item.id}}">
{{item.name}}
</option>
</select>
</div>
<div class="col-lg-12">
<p style="float:left;">Fields marked with <span class="redstar"><b>*</b></span> are required.</p></br>
</div>
</div>
</form></div>
<div class="modal-footer"><button type="button" class="btn btn-danger" data-dismiss="modal">Close</button><button type="button" class="btn btn-success" data-dismiss="modal" ng-click="registerFunction()">Sign Up</button></div>
</div></div>
</div>
This is how i execute it :
<?php
include_once 'config.php';
$data = json_decode(file_get_contents("php://input"));
$firstname = $data->firstname;
$lastname = $data->lastname;
$login = $data->login;
$password = $data->password;
$email = $data->email;
$city = $data->city;
$postalcode = $data->postalcode;
$adress = $data->adress;
$country = $data->country;
$dbh->query("INSERT INTO `accounts` (`account_id`, `firstname`, `lastname`, `login`, `password`, `email`, `city`, `postalcode`, `adress`, `country`, `role`)
VALUES (NULL,'".$firstname."','".$lastname."','".$login."',MD5('".$password."'),'".$email."','".$city."','".$postalcode."','".$adress."','".$country."', 0) ") or die(mysql_error());
$dbh = null;
?>
And this is my connection :
<?php
$hostname='localhost';
$username='root';
$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=myshop",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
echo 'Connected to Database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
My question is how can i for example add and If
in my controller like there is an error duplicate entry for 'login' i do something in my angular. So how can i catch the error in to my controller?
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'login'' in C:\xampp2\htdocs\MyOwnShop\php\registration.php:16 Stack trace: #0 C:\xampp2\htdocs\MyOwnShop\php\registration.php(16): PDO->query('INSERT INTO `ac...') #1 {main} thrown in C:\xampp2\htdocs\MyOwnShop\php\registration.php on line 16
` And my console doesn't show anything. – SupremeDEV Mar 16 '18 at 09:00