-2
Next, I am registering cep and I need to get the id of this cep, recently registered to insert in the table of users. I tried to do in Pdo using the same php file that I registered the cep... Gave a blow, for appeared message on the console saying that did not recognize the variables of the zip. I’m thinking of registering users, separate from the zip code, but as I "take" the id of the zip code to the other php file?
angular:
angular.module('app.controllers', [])
.controller('loginCtrl', function ($scope) {
})
.controller('enderecoCtrl', function ($scope, $http, $location) {
$scope.adicionarEndereco = function (endereco) {
$http.post("php/salvaEndereco.php", endereco).success(function (data){
});
$location.path('/cadastraUsuario');
}
})
.controller('usuarioCtrl', function ($scope, $http) {
$scope.pegaUsuario = function (usuario) {
$http.post("php/salvaEndereco.php", usuario).success(function (data){
console.log(data);
});
}
})
php:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,X-Prototype-Version,X-Requested-With');
include_once("conPDO.php");
$pdo = conectar();
$data = file_get_contents("php://input");
$data = json_decode($data);
$cep = $data->cep;
$uf = $data->uf;
$cidade = $data->cidade;
$bairro = $data->bairro;
$logradouro = $data->logradouro;
$insereEndereco=$pdo->prepare("INSERT INTO cep (idCep, cep, uf, cidade, bairro, logradouro) VALUES (?, ?, ?, ?, ?, ?)");
$insereEndereco->bindValue(1, NULL);
$insereEndereco->bindValue(2, $cep);
$insereEndereco->bindValue(3, $uf);
$insereEndereco->bindValue(4, $cidade);
$insereEndereco->bindValue(5, $bairro);
$insereEndereco->bindValue(6, $logradouro);
$insereEndereco->execute();
$idCep = $pdo->lastInsertId();
?>
Which bank does it use?
– Marconi
Why do this at Angular? If you only need the last one, select in php to return only the last one, instead of returning the whole list and then filter with js. More practical. Unless other data is used for another purpose.
– celsomtrindade
I am using mysql
– GustavoSevero
@Celsomtrindade, thanks for the tip, but I’m already doing it another way... pick it back from the localStorage and insert it along with the user object... Get it? hehehehe
– GustavoSevero
0_0 I think that returning by php is more practical, because php has the function of catching the last registered value. You don’t need to go around so much, it is less prone to errors and the application is lighter. = D Da a procurada por ai ;)
– celsomtrindade
But how will you select in the bank to get the last id, when there are several registered zip codes?
– GustavoSevero
PHP is not my area, but in a recent project, the backend people made a return of this, where it takes the last value registered in the database and returns. Just take care not to take the risk of there being simultaneous Insert. Very unlikely to be, but it is always good to check.
– celsomtrindade