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.
– Rodrigo Tognin
Another interesting thing is to tell us exactly what is going wrong. Which part of the code and which error messages appear.
– Rodrigo Tognin
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.
– Wanderson Silva
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– adventistaam
Gives a
print_r( $sql->errorInfo() )
, right after the execution of the functionexisteEmail
– adventistaam
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 )
– Wanderson Silva
@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?.
– Augusto Vasques