Created
September 26, 2013 13:59
-
-
Save netojoaobatista/6714651 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function excluirConvidado($idPerfil, $id) | |
{ | |
/** | |
* Evite utilizar globais. É ruim. | |
* Em vez da global, prefira passar o $bd como parâmetro, | |
* especificando seu tipo. | |
*/ | |
global $bd; | |
/** | |
* Todo o bloco de código abaixo foi comentado, pois é inútil. | |
* Veja que você está selecionando, na tabela Convidados, um | |
* determinado idPerfil. | |
* | |
* Logo abaixo, você faz o delete na mesma tabela Convidados, | |
* verificando o mesmo idPerfil. Exatamente pelo fato do DELETE | |
* ser na mesma tablea, com a mesma condição, o SELECT se faz | |
* desnecessário. | |
* | |
* $sql = ' SELECT id FROM Convidados WHERE idPerfil = :idPerfil '; | |
* | |
* try { | |
* $query = $bd->prepare($sql); | |
* $query->bindValue(':idPerfil', $idPerfil, PDO::PARAM_INT); | |
* $query->execute(); | |
* | |
* $permissao = $query->fetchAll(PDO::FETCH_NUM); | |
* | |
* } catch (Exception $e) { | |
* echo $e->getMessage(); | |
* } | |
*/ | |
/** | |
* O código abaixo é o que realmente interessa. | |
* Veja que o DELETE vai apagar um determinado id se tiver o mesmo idPerfil. | |
* Você não precisa do SELECT do bloco acima, só esse DELETE já faz o trabalho. | |
*/ | |
$sql = ' DELETE FROM Convidados WHERE id = :id AND idPerfil = :idPerfil'; | |
try { | |
$query = $bd->prepare($sql); | |
$query->bindValue(':id', $id, PDO::PARAM_INT); | |
$query->bindValue(':idPerfil', $idPerfil, PDO::PARAM_INT); | |
/** | |
* O return do execute vai dizer se a operação foi bem sucedida | |
*/ | |
return $query->execute(); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment