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;
}
}
}