Ir para conteúdo

tekton

Members
  • Total de itens

    21
  • Registro em

  • Última visita

Posts postados por tekton


  1. Preciso fazer um CRUD de teste para tentar uma vaga em uma empresa. Nesse teste, tive que criar os campos Nome, Email, Telefone e Endereço, e deveria ser possível inserir mais de um endereço por nome nesse CRUD. Acho que não da pra simplesmente criar outro campo para inserir outro endereço. Acho que o objetivo do teste é que eu crie relacionamento de tabelas, mas não faço ideia de como começar. Estou fazendo o CRUD com PHP e já tenho tudo pronto, adicionar, editar e excluir. Já está tudo funcionando. Só falta agora essa opção pra poder colocar mais de um endereço por nome. Vou colocar o código da classe e do BD. Se precisarem de mais algum é só avisar. Agradeço de antemão pela ajuda.

     

    -- phpMyAdmin SQL Dump
    -- version 4.8.3
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost:3306
    -- Generation Time: Nov 16, 2019 at 11:19 PM
    -- Server version: 5.7.24
    -- PHP Version: 7.3.7
    
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET AUTOCOMMIT = 0;
    START TRANSACTION;
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;
    
    --
    -- Database: `crudoo`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `contatos`
    --
    
    CREATE TABLE `contatos` (
      `id` int(11) UNSIGNED NOT NULL,
      `nome` varchar(100) DEFAULT NULL,
      `email` varchar(100) NOT NULL DEFAULT '',
      `telefone` varchar(20) NOT NULL,
      `endereco` varchar(100) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    --
    -- Dumping data for table `contatos`
    --
    
    INSERT INTO `contatos` (`id`, `nome`, `email`, `telefone`, `endereco`) VALUES
    (2, 'test2e', 'teste2@teste.com', '316497', 'rua a'),
    (4, 'wanderson', 'sonwander@yahoo.com.br', '985930559', 'rua brasil'),
    (6, 'andrezinho', 'andre@site.com', '2222222222', 'rua j');
    
    --
    -- Indexes for dumped tables
    --
    
    --
    -- Indexes for table `contatos`
    --
    ALTER TABLE `contatos`
      ADD PRIMARY KEY (`id`);
    
    --
    -- AUTO_INCREMENT for dumped tables
    --
    
    --
    -- AUTO_INCREMENT for table `contatos`
    --
    ALTER TABLE `contatos`
      MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
    COMMIT;
    
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    <?php
    class Contato {
    
        private $pdo;
    
        public function __construct() {
            $this->pdo = new PDO("mysql:dbname=crudoo;host=localhost", "root", "root");
        }
    
        public function adicionar($email, $nome, $telefone, $endereco) {
            if($this->existeEmail($email) == false) {
                $sql = "INSERT INTO contatos (nome, email, telefone, endereco) VALUES (:nome, :email, :telefone, :endereco)";
                $sql = $this->pdo->prepare($sql);
                $sql->bindValue(':nome', $nome);
                $sql->bindValue(':email', $email);
                $sql->bindValue(':telefone', $telefone);
                $sql->bindValue(':endereco', $endereco);
                $sql->execute();
    
                return true;
            } else {
                return false;
            }
        }
    
        public function getInfo($id) {
            $sql = "SELECT * FROM contatos WHERE id = :id";
            $sql = $this->pdo->prepare($sql);
            $sql->bindValue(':id', $id);
            $sql->execute();
    
            if($sql->rowCount() > 0) {
                return $sql->fetch();
            } else {
                return array();
            }
        }
    
        public function getAll() {
            $sql = "SELECT * FROM contatos";
            $sql = $this->pdo->query($sql);
    
            if($sql->rowCount() > 0) {
                return $sql->fetchAll();
            } else {
                return array();
            }
        }
    
        public function editar($nome, $email, $telefone, $endereco, $id) {
            if($this->existeEmail($email) == false){
                $sql = "UPDATE contatos SET nome = :nome, email = :email, telefone = :telefone, endereco = :endereco WHERE id = :id";
                $sql = $this->pdo->prepare($sql);
                $sql->bindValue(':nome', $nome);
                $sql->bindValue(':email', $email);
                $sql->bindValue(':telefone', $telefone);
                $sql->bindValue(':endereco', $endereco);
                $sql->bindValue(':id', $id);
                $sql->execute();
    
                return true;
    
                } else {
                    return false;
                }
        }
    
        public function excluir($id) {
                $sql = "DELETE FROM contatos WHERE id = :id";
                $sql = $this->pdo->prepare($sql);
                $sql->bindValue(':id', $id);
                $sql->execute();
            }
    
        private function existeEmail($email) {
            $sql = "SELECT * FROM contatos WHERE email = :email";
            $sql = $this->pdo->prepare($sql);
            $sql->bindValue(':email', $email);
            $sql->execute();
    
            if($sql->rowCount() > 0) {
                return true;
            } else {
                return false;
            }
        }
    }

     


  2. Olá pessoal, uma empresa pediu pra fazer um teste e mandou fazer um CRUD com Nome, Email, Telefone e Endereço. Seguindo as aulas da parte de CRUD do Bonieky Lacerda tentei incluir mais dois itens mas não está dando certo. Não sei o que posso estar fazendo errado. Vou colocar os códigos para mostrar.

     

    // BANCO 
    
    -- phpMyAdmin SQL Dump
    -- version 4.9.0.1
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost:8889
    -- Tempo de geração: 07/11/2019 às 17:05
    -- Versão do servidor: 5.7.26
    -- Versão do PHP: 7.3.8
    
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    
    --
    -- Banco de dados: `teste`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Estrutura para tabela `tab_teste`
    --
    
    CREATE TABLE `tab_teste` (
      `id` int(11) NOT NULL,
      `nome` varchar(100) NOT NULL,
      `email` varchar(50) NOT NULL,
      `telefone` int(20) NOT NULL,
      `endereco` varchar(100) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    --
    -- Índices de tabelas apagadas
    --
    
    --
    -- Índices de tabela `tab_teste`
    --
    ALTER TABLE `tab_teste`
      ADD PRIMARY KEY (`id`);
    
    --
    -- AUTO_INCREMENT de tabelas apagadas
    --
    
    --
    -- AUTO_INCREMENT de tabela `tab_teste`
    --
    ALTER TABLE `tab_teste`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
    // INDEX DO CRUD
    
    
    <?php
    include 'teste.class.php';
    	
    $teste = new Teste();
    ?>
    
    <h1>teste</h1>
    
    <table border="1" width="500">
    	<tr>
    	<th>ID</th>
    		<th>Nome</th>
    
    	<th>Email</th>
    
    	<th>Telefone</th>
    		<th>Endereço</th>
    
    </tr>
    <?php
    $lista = $teste->getAll();
    	foreach ($lista as $item):
    ?>
    <tr>
    	<td><?php echo $item['id']; ?></td>
    	<td><?php echo $item['nome']; ?></td>
    	<td><?php echo $item['email']; ?></td>
    	<td><?php echo $item['telefone']; ?></td>
    	<td><?php echo $item['endereco']; ?></td>
    </tr>
    	<?php endforeach; ?>
    </table>
    // CRUD
    
    
    <?php
    class Teste {
    
    	private $pdo;
    
    	public function __construct(){
    
    		$this->pdo = new PDO("mysql:dbname=teste;host=localhost", "root","root");
    		
    			}
    			public function adicionar($nome, $email, $telefone, $endereco){
    
    				if($this->existeEmail($email) == false){
    					$sql = "INSERT INTO tab_teste (nome, email, telefone, endereco) VALUES (:nome, :email, :telefone, :endereco)";
    					$sql = $this->pdo->prepare($sql);
    					$sql->bindValue(':nome', $nome);
    					$sql->bindValue(':email', $email);
    					$sql->bindValue(':telefone', $telefone);
    					$sql->bindValue(':endereco', $endereco);
    					$sql->execute();
    
    					return true;
    				} else{
    					return false;
    				}
    			}
    
    			public function getNome($email){
    				$sql = "SELECT nome FROM tab_teste WHERE email = :email";
    				$sql = $this->pdo->prepare($sql);
    				$sql->bindValue(':email', $email);
    				$sql->execute();
    
    				if($sql->rowCont() > 0){
    					$info = $sql->fetch();
    
    					return $info['nome'];
    				}else {
    					return '';
    				}
    			}
    
    			public function getAll(){
    				$sql = "SELECT * FROM tab_teste";
    				$sql = $this->pdo->query($sql);
    
    				if($sql->rowCont() > 0){
    					return $sql->fetchAll();
    				}else{
    
    					return array();
    				}
    			}
    
    			public function editar($nome, $email) {
    				if($this->existeEmail($email) == true){
    					$sql = "UPDATE tab_teste SET nome = :nome WHERE email = :email";
    					$sql = $this->pdo->prepare($sql);
    					$sql->bindValue(':nome', $nome);
    					$sql->bindValue(':email', $email);
    					$sql->bindValue(':telefone', $telefone);
    					$sql->bindValue(':endereco', $endereco);
    					$sql->execute();
    
    					return true;
    
    				}else{
    					return false;
    				}
    			}
    
    			public function excluir($email){
    				if($this->existeEmail($email)){
    					$sql = "DELETE FROM tab_teste WHERE email = :email";
    					$sql = $this->pdo->prepare($sql);
    					$sql->bindValue(':email', $email);
    					$sql->execute();
    
    					return true;
    				} else {
    					return false;
    				}
    			}
    
    			private function existeEmail($email){
    				$sql = "SELECT FROM tab_teste WHERE email = :email";
    				$sql = $this->pdo->prepare($sql);
    				$sql->bindValue(':email', $email);
    				$sql->execute();
    
    				if($sql->rowCont() > 0){
    					return true;
    				}else {
    					return false;
    				}
    			}
    }

     

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.