-1
Hello,
I have a form, simple, with 3 fields, name, email and password in HTML and along with an Angularjs code. However, when submitting the form, I get the message that the data sent to the bank, but they are not there in the same.
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>CRUD AngularJS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js></script>
</head>
<body ng-controller="MainCtrl as ctrl">
<form ng-submit="ctrl.submit()">
Nome<br>
<input type="text" ng-model="ctrl.user.nome"><br>
Email<br>
<input type="text" ng-model="ctrl.user.email"><br>
Senha<br>
<input type="password" ng-model="ctrl.user.senha"><br>
<input type="submit" id="submit" value="Submit" />
</form>
<script type="text/javascript">
angular.module('myApp', [])
.controller('MainCtrl', ['$scope', '$http', function($scope, $http){
$scope.list = [];
var self = this;
self.submit = function() {
console.log('User clicked submit with ', self.user);
$http.post('php/salvar.php', {'nome': $scope.nome, 'email': $scope.email, 'senha': $scope.senha})
.then(function(response) {
console.log(response.status);
console.log(response.data.msg);
}, function(response) {
console.log(response.status);
console.log(response.msg);
});
}
}]);
</script>
</body>
</html>
PHP:
?php
$user = 'root';
$password = 'root';
$db = 'angularDB';
$host = 'localhost';
$con = mysqli_connect("localhost", $user, $password, $db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$nome = $_POST['nome'];
$email = $_POST['email'];
$senha = $_POST['senha'];
$ins = mysqli_query($con, "INSERT INTO users VALUES (NULL, '$nome', '$email', '$senha')");
if($ins == 1){
echo json_encode( array('status' => 200, 'msg' => 'Cadastro efetuado com sucesso!'));
}else{
echo json_encode( array('status' => 0, 'msg' => 'Erro ao cadastrar no banco.'));
}
?>
What it returns in the request answer?
– henriquedpereira
For security reasons, use Prepared statements in queries, especially those involving strings, helping to deliver SQL Injection. What is the value of the variable
$ins
after the query? Does the PHP/Apache/Nginx error log have any error messages? Try enabling error display to get a better sense of what’s going on.– Vinícius Gobbo A. de Oliveira
Ta very simple and visible the first problem, working or not
– Otto
Sorry @Otto, how could I treat this?
– GustavoSevero
@Viníciusgobboa.deOliveira, I don’t know how to use Prepared statement with mysqli. Can help me?
– GustavoSevero
@Gustavosevero the way this in the answer
– Otto
Well @Otto, I corrected the treatment. I put it in the post edition, check it out. And what happens is that only the code is inserted in the bank, that is, only the code field is filled, the other fields are blank... Very strange.
– GustavoSevero
From what I’ve noticed, the problem is not in php but in Angular.
– GustavoSevero
Note that in your angular include code is breaking.
– Ivan Ferrer
Ready @Ivanferrer, include fixed. But here on my computer, there is no such break and yet, nothing.
– GustavoSevero
Still broken... rrsrsrs, missing quotes
– Ivan Ferrer