CRUD-help with code [closed]

Closed. this question is out of scope and is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 1 year ago .

improve this question

A company asked to take a test and had a CRUD with name, Email, telephone and address. Following the lessons from CRUD I tried to include two more items but it is not working. No I know what I might be doing wrong. I'll put the codes to show.

/ / Bank

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

/ / 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>



<?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;
                }
            }
}
Author: Daniel Mendes, 2019-11-07

1 answers

The problem may be in the existeEmail () function, more precisely in the line of query. Because of this error, a syntax error is generated that terminates the script and consequently, it is not progressing to what to do after checking the email.

$sql = "SELECT FROM tab_teste WHERE email = :email";

Is missing you say what you want to select. You must indicate the name of the column right after the word SELECT of query. If it's all the record data, put the *, if it's just the email, put the column name of the email. It should look like this:

$sql = "SELECT * FROM tab_teste WHERE email = :email";

Or

$sql = "SELECT email FROM tab_teste WHERE email = :email";
 1
Author: Leonardo Furlan, 2019-11-09 13:04:19