Created
March 24, 2016 17:08
-
-
Save silvadias22/4f4de88bbff33b57c686 to your computer and use it in GitHub Desktop.
Crud - PHP MySQL (Simples)
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
| drop database crudRelacionamento; | |
| create database crudRelacionamento; | |
| use crudRelacionamento; | |
| -- Tabela para armazenamento dos dados (Editora). | |
| CREATE TABLE Editora ( | |
| idEditora int NOT NULL PRIMARY KEY AUTO_INCREMENT, | |
| nome VARCHAR(30) NOT NULL, | |
| site VARCHAR(30) NOT NULL, | |
| email VARCHAR(30) NOT NULL, | |
| telefone VARCHAR(40) NOT NULL | |
| ); | |
| CREATE TABLE Autor ( | |
| idAutor int NOT NULL PRIMARY KEY AUTO_INCREMENT, | |
| idEditora int NOT NULL, | |
| nome VARCHAR(30)NOT NULL, | |
| site VARCHAR(30) , | |
| email VARCHAR(30)NOT NULL, | |
| publicado VARCHAR(30)NOT NULL | |
| ); | |
| ALTER TABLE Autor | |
| ADD CONSTRAINT fk_Editora_Autor | |
| FOREIGN KEY (idEditora) REFERENCES Editora (idEditora) | |
| ON DELETE CASCADE; |
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
| <?php | |
| /* CRUD - ISUD - by: Fabrício da Silva Dias | |
| ____________________________________________________________ | |
| || || | |
| || crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy || | |
| || isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete || | |
| ||__________________________________________________________|| | |
| */ | |
| // Função para conexão com banco de dados. | |
| function conexaoPDO(){ | |
| #Iniciando conexão com banco de dados | |
| $pdo = new PDO ('mysql:dbname=crudRelacionamento','root', ''); | |
| $pdo->setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| return $pdo; | |
| } | |
| function inserirEditora($nome, $site, $email, $telefone){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare(' | |
| INSERT INTO Editora( | |
| nome, | |
| site, | |
| email, | |
| telefone | |
| )VALUES( | |
| :nome, | |
| :site, | |
| :email, | |
| :telefone | |
| );' | |
| ); | |
| $stmt->bindValue(":nome", $nome); | |
| $stmt->bindValue(":site", $site); | |
| $stmt->bindValue(":email", $email); | |
| $stmt->bindValue(":telefone", $telefone); | |
| $stmt->execute(); | |
| return $pdo->lastInsertId(); | |
| } | |
| # Função para efetuar buscas, essa função busca todas Editoras. | |
| function buscarTodasEditoras(){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare("select * from Editora;"); | |
| $stmt->execute(); | |
| $pdo = null; | |
| return $stmt; | |
| } | |
| #função para efetuar buscas de registro específicado pelo ID; | |
| function buscarEditora($idEditora){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare('SELECT FROM Editora WHERE idEditora = :idEditora;'); | |
| $stmt->bindValue(":idEditora", $idEditora); | |
| $stmt->execute(); | |
| $pdo = null; | |
| return $stmt; | |
| } | |
| #Função de inserção, utilizada para inserir os dados preenchidos no formulario "Autor" na tabela "Autor" do banco de dados. | |
| function inserirAutor($idEditora, $nome, $site, $email, $publicado){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare(' | |
| INSERT INTO Autor( | |
| idEditora, | |
| nome, | |
| site, | |
| email, | |
| publicado | |
| )VALUES( | |
| :idEditora, | |
| :nome, | |
| :site, | |
| :email, | |
| :publicado | |
| );' | |
| ); | |
| $stmt->bindValue(":idEditora", $idEditora); | |
| $stmt->bindValue(":nome", $nome); | |
| $stmt->bindValue(":site", $site); | |
| $stmt->bindValue(":email", $email); | |
| $stmt->bindValue(":publicado", $publicado); | |
| $stmt->execute(); | |
| $pdo = null; | |
| } | |
| function buscarEditoraPorID($idEditora){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare('select * from Editora WHERE idEditora = :idEditora;'); | |
| $stmt->bindValue(":idEditora", $idEditora); | |
| $stmt->execute(); | |
| $pdo = null; | |
| return $stmt; | |
| } | |
| #Função para buscar todos os Autores, sem especificação. | |
| function buscarTodosAutores(){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare("select * from Autor;"); | |
| $stmt->execute(); | |
| $pdo = null; | |
| return $stmt; | |
| } | |
| #Função para buscar os registros de Autores especificos utilizando ID do autor. | |
| function buscarAutorPorID($idAutor){ | |
| $pdo = conexaoPDO(); | |
| $stm = $pdo->prepare("select * from Autor WHERE idAutor = :idAutor;"); | |
| $stm->bindValue(":idAutor", $idAutor); | |
| $stm->execute(); | |
| $pdo = null; | |
| return $stm; | |
| } | |
| #Função para buscar os registros de Autores pelo ID da Editora. | |
| function buscarAutorPeloIdEditora($idEditora){ | |
| $pdo = conexaoPDO(); | |
| $stm = $pdo->prepare("select * from Autor WHERE idEditora = :idEditora;"); | |
| $stm->bindValue(":idEditora", $idEditora); | |
| $stm->execute(); | |
| $pdo = null; | |
| return $stm; | |
| } | |
| // função para atualizar \ Update das informações da Editora. | |
| function updateEditora($nome, $site, $email, $telefone, $idEditora){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare( | |
| 'update Editora '. | |
| 'set nome = :nome,'. | |
| 'site = :site,'. | |
| 'email = :email, '. | |
| 'telefone = :telefone, '. | |
| 'where idEditora = :idEditora'); | |
| $stmt->bindValue(":nome", $nome); | |
| $stmt->bindValue(":site", $site); | |
| $stmt->bindValue(":email", $email); | |
| $stmt->bindValue(":telefone", $telefone); | |
| $stmt->bindValue(":idEditora", $idEditora); | |
| $stmt->execute(); | |
| $pdo = null; | |
| } | |
| #Função para efetuar a deleção de Autor especificado pelo ID do Autor, especifico. | |
| function deletarAutor($idAutor){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare('DELETE FROM Autor WHERE idAutor = :idAutor;'); | |
| $stmt->bindValue(":idAutor", $idAutor); | |
| $stmt->execute(); | |
| $pdo = null; | |
| return $stmt; | |
| } | |
| #Função para deletar todos os autores de uma editora. | |
| function deletarAutoresEditora($idEditora){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare('DELETE FROM Autor WHERE idAutor = :idEditora;'); | |
| $stmt->bindValue(":idEditora", $idAutor); | |
| $stmt->execute(); | |
| $pdo = null; | |
| return $stmt; | |
| } | |
| #Função utilizada para atualizar(Update) Autor. | |
| function atualizarAutor($nome, $site, $email, $publicado $idAutor){ | |
| $pdo = conexaoPDO(); | |
| $stmt = $pdo->prepare( | |
| 'update Autor '. | |
| 'set nome = :nome,'. | |
| 'site = :site,'. | |
| 'email = :email,'. | |
| 'publicado = :publicado,'. | |
| 'where idAutor = :idAutor'); | |
| $stmt->bindValue(":nome", $nome); | |
| $stmt->bindValue(":site", $site); | |
| $stmt->bindValue(":email", $email); | |
| $stmt->bindValue(":publicado", $publicado); | |
| $stmt->bindValue(":idAutor", $idAutor); | |
| $stmt->execute(); | |
| $pdo = null; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment