public function salvar(\Aluno $u) { $cpf = $u->getCpf(); $rg = $u->getRg(); $data_nascimento = $u->getData_nascimento(); $nome = $u->getNome(); $telefone = $u->getTelefone(); if ($u->getCod_aluno()) { $id = $u->getCod_aluno(); $sql = "update aluno set cpf=:cpf, rg=:rg, data_nascimento=:data_nascimento, nome=:nome, telefone=:telefone where cod_aluno = :id"; } else { $id = $this->generateID(); $u->setCod_aluno($id); $sql = "insert into aluno (cod_aluno, cpf, rg, data_nascimento, nome, telefone) values (:id, :cpf, :rg, :data_nascimento, :nome, :telefone)"; } $cnx = Conexao::getConexao(); $sth = $cnx->prepare($sql); $sth->bindParam("id", $id); $sth->bindParam("cpf", $cpf); $sth->bindParam("rg", $rg); $sth->bindParam("data_nascimento", $data_nascimento); $sth->bindParam("nome", $nome); $sth->bindParam("telefone", $telefone); try { $sth->execute(); return $u; } catch (Exception $exc) { echo $exc->getMessage(); } }
public function salvar() { $u = new Aluno(); $codigo = isset($_POST['codigo']) ? $_POST['codigo'] : FALSE; if (trim($codigo) != "") { $u->setCod_aluno($codigo); } // Verifica no post $cpf = isset($_POST['cpf']) ? $_POST['cpf'] : FALSE; if (!$cpf || trim($cpf) == "") { throw new Exception("O campo cpf è Obrigatorio!"); } $rg = isset($_POST['rg']) ? $_POST['rg'] : FALSE; $data_nascimento = isset($_POST['data_nascimento']) ? $_POST['data_nascimento'] : FALSE; $nome = isset($_POST['nome']) ? $_POST['nome'] : FALSE; $telefone = isset($_POST['telefone']) ? $_POST['telefone'] : FALSE; $u->setCpf($cpf); $u->setRg($rg); $u->setData_nascimento($data_nascimento); $u->setNome($nome); $u->setTelefone($telefone); $du = new DaoAluno(); $usu = $du->salvar($u); if ($usu instanceof aluno) { // Se gravado no banco, entao vai para a pagina inicial header("location: " . URL . "pagina-inicial"); } else { echo 'Não foi possivel salvar o Aluno'; } }