How to get the last id generated in php using angular?

Asked

Viewed 252 times

-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?

  • 2

    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.

  • I am using mysql

  • @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

  • 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 ;)

  • But how will you select in the bank to get the last id, when there are several registered zip codes?

  • 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.

Show 2 more comments

1 answer

0

Gustavo, if your Bank always brings a unique id for each zip code, you just have to do the SELECT that searches for the last registered id, ordering by id, even, DESC. This way you search for the latest. Then, in the insertion, put a rule of +1 and voilà! : )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.