CRUD - code help

Asked

Viewed 230 times

0

a company asked to take a test and ordered a CRUD with Name, Email, Telephone and Address. Following the lessons from CRUD I tried to include two more items but it’s not working. I don’t know what I might be doing wrong. I’ll put the codes to show.

// BENCH

-- 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;
                }
            }
}
  • Your "Id" field is not set to auto-incremental. Either you make a function to increment it to Insert home, or you adjust it to be auto-incremental.

  • Another interesting thing is to tell us exactly what is going wrong. Which part of the code and which error messages appear.

  • Friend, thanks for the help. I removed this part of the code. I will edit for you to see. I have already put auto-increment.

  • Also try to rename variables, where querys (select, Insert, etc.), be $sql and where to receive the configuration ($this->pdo->prepare($sql);) be $stmt, whether it would be statement

  • Gives a print_r( $sql->errorInfo() ), right after the execution of the function existeEmail

  • I gave the print_r and the result was this: Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'FROM tab_test WHERE email = '[email protected]'' at line 1 )

  • @Wandersonsilva, this question has an answer to the proposed problem. If this answer has helped you consider it correct. If you do not know how to accept an answer read: How and why to accept an answer?.

Show 2 more comments

1 answer

1

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

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

You are missing say what you want to select. You must indicate the column name right after the word SELECT of query. If this is all the data in the log, put the *, if it’s just the email, put the name of the email column. It should stay that way:

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

Or

$sql = "SELECT email FROM tab_teste WHERE email = :email";
  • Do I have to complete the parameters? This way: $sql = "SELECT FROM tab_test WHERE name = :name, email = :email, etc";

  • I edited my answer for you to understand better. What is missing is to indicate the column name right after the query’s SELECT word.

  • After the keyword SELECT or comes * or the name(s) of the (s) field(s) you wish to select.

  • Friends I will try here. Thanks for now.

  • Friends, I managed to solve the problem I had here. Thanks for the tips. Now I need to enter more than one address per name. I think I should use Foreign key is not?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.