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

-2

Next, I'm cadastering zip and I need to get the id of this zip, newly registered to insert in the users table. I tried to do in pdo using the same php file that I registered the zip ... Deu pau, because it appeared message in the console saying that it did not recognize the variables of the zip. I'm thinking of doing user registration, separate from ceps, but how do I "take" the zip id 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();
?>
    
asked by anonymous 27.01.2016 / 21:58

1 answer

0

Gustavo, if your bank always brings a unique id for each zip, just do the SELECT that searches for the last id registered, ordering by id, even, DESC. That way you search for the latest one. Then, in the insert, put a +1 and voilà! :)

    
28.01.2016 / 11:20