Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá boa tarde estou com problema para retorna o " lastInsertId() ", não sei em que parte estou errando?
class Conexao{
//ATRIBUTOS
private $usuario;
private $senha;
private $servidor;
private $banco;
//CONTRUTOR
public function __construct(){
$this->usuario = 'root';
$this->senha = '';
$this->servidor = 'localhost';
$this->banco = 'teste_sistemas';
}
//METODOS
public function conecta(){
return new PDO('mysql:host='.$this->servidor.';dbname='.$this->banco, $this->usuario, $this->senha);
}
public function retornaId(){
return $this->conecta()->lastInsertId();
}
}
require_once 'Conexao.class-pdo.php';
class Funcionario{
//ATRIBUTOS
private $con;
private $nome;
private $cpf;
private $senha;
//METODOS MÁGICOS
public function __construct(){//CONSTRUTOR
$this->con = new Conexao();
}
public function __set($atributo, $value){//SET atributo que receberá o valor
$this->$atributo = $value;
}
public function __get($atributo){//GET capturar o valor armazenado
return $this->$atributo;
}
//METODOS
public function queryInsertFuncionario(){//CADASTRANDO O FUNCIONARIO $data
$this->nome = $_POST['nome'];
$this->cpf = $_POST['cpf'];
$this->senha = sha1($_POST['senha']);
$rst = $this->con->conecta()->prepare("INSERT INTO `funcionario` (`nome`, `cpf`, `senha`) VALUES (:nome, :cpf, :senha);");
$rst->bindParam(':nome', $this->nome, PDO::PARAM_STR);
$rst->bindParam(':cpf', $this->cpf, PDO::PARAM_STR);
$rst->bindParam(':senha', $this->senha, PDO::PARAM_STR);
if($rst->execute()){
$idGerado = $this->con->retornaId();
echo '<script type="text/javascript">alert("'.$idGerado.'");</script>';
}
}
}
Fiz até dessa maneira, mais e está dados erro?
public function queryInsertFuncionario(){//CADASTRANDO O FUNCIONARIO $data
$this->nome = $_POST['nome'];
$this->cpf = $_POST['cpf'];
$this->senha = sha1($_POST['senha']);
$rst = $this->con->conecta()->prepare("INSERT INTO `funcionario` (`nome`, `cpf`, `senha`) VALUES (:nome, :cpf, :senha);");
$rst->bindParam(':nome', $this->nome, PDO::PARAM_STR);
$rst->bindParam(':cpf', $this->cpf, PDO::PARAM_STR);
$rst->bindParam(':senha', $this->senha, PDO::PARAM_STR);
if($rst->execute()){
$idGerado = $rst->lastInsertId();
echo '<script type="text/javascript">alert("'.$idGerado.'");</script>';
}
}Carregando comentários...