0
While executing the code below, more than one record is deleted from the database. I cannot find the problem.
Code to delete
excluir_client.php
<?php
require 'repositorio_clientes.php';
$clientes = $repositorio->getListarClientes();
echo"< br>< br>< br>< br>< br>< br>";
while($clienteTemporario = array_shift($clientes)){
if($clienteTemporario->getCodigo() == $_REQUEST['codigo']){
echo '<center>' . "Cliente: " . $clienteTemporario->getNome() . ", cpf: " . $clienteTemporario->getCpf() . $repositorio->excluirClientes($_REQUEST['codigo']) . "excluído com sucesso" . '</center>';
}
}
exit;
?>
Page: repositorio_clients.php
<? php
require ' conexao.php ';
include 'cliente.php';
interface IRepositorioClientes {
public function cadastrarClientes($cliente);
public function excluirClientes($cliente);
public function atualizarClientes($cliente);
public function buscarCliente($codigo);
public function getListarClientes();
public function getListarClienteDia();
public function getListarClienteAtraso();
}
class RepositorioClientesMySQL implements IRepositorioClientes {
private $conexao;
public function __construct() {
$this->conexao = new Conexao("localhost", "root", "", "biblioteca");
if($this->conexao->conectar() == false) {
echo "Erro" . mysqli_error();
}
}
public function cadastrarClientes($cliente) {
$nome = $cliente->getNome();
$endereco = $cliente->getEndereco();
$cpf = $cliente->getCpf();
$saldo = $cliente->getSaldo();
$situacao = $cliente->getSituacao();
$data= $cliente->getData();
$sql = "INSERT INTO cliente (nome, endereco, cpf, saldo, situacao, data) VALUES
('$nome', '$endereco', '$cpf', '$saldo', '$situacao', '$data')";
$this->conexao->executarQuery($sql);
}
public function excluirClientes($codigo) {
$sql = "DELETE FROM cliente WHERE codigo = '$codigo'";
$this->conexao->executarQuery($sql);
}
public function atualizarClientes($cliente) {
$nome = $cliente->getNome();
$codigo = $cliente->getCodigo();
$cpf = $cliente->getCpf();
$endereco = $cliente->getEndereco();
$saldo = $cliente->getSaldo();
$situacao = $cliente->getSituacao();
$data = $cliente->getData();
$linha = $this->conexao->obtemPrimeiroRegistroQuery;
$sql = "UPDATE cliente SET nome ='$nome', endereco='$endereco', cpf='$cpf', saldo='$saldo', situacao='$situacao', data='$data' WHERE codigo ='$codigo'";
$this->conexao->executarQuery($sql);
}
public function buscarCliente($codigo) {
$linha = $this->conexao->obtemPrimeiroRegistroQuery ("SELECT * FROM cliente WHERE codigo='$codigo'");
$cliente = new Cliente(
$linha['nome'],
$linha['codigo'],
$linha['endereco'],
$linha['cpf'],
$linha['saldo'],
$linha['situacao'],
$linha['data']);
return $cliente;
}
public function getListarClientes() {
$listagem = $this->conexao->executarQuery("SELECT * FROM cliente");
$arrayClientes = array();
while($linha = mysqli_fetch_array($listagem)){
$cliente = new Cliente(
$linha['nome'],
$linha['codigo'],
$linha['endereco'],
$linha['cpf'],
$linha['saldo'],
$linha['situacao'],
$linha['data']);
array_push($arrayClientes, $cliente);
}
return $arrayClientes;
}
public function getListarClienteDia() {
$listagem = $this->conexao->executarQuery("SELECT * FROM cliente WHERE situacao='Em Dia'");
$arrayClientes = array();
while($linha = mysqli_fetch_array($listagem)){
$cliente = new Cliente(
$linha['nome'],
$linha['codigo'],
$linha['endereco'],
$linha['cpf'],
$linha['saldo'],
$linha['situacao'],
$linha['data']);
array_push($arrayClientes, $cliente);
}
return $arrayClientes;
}
public function getListarClienteAtraso() {
$listagem = $this->conexao->executarQuery("SELECT * FROM cliente WHERE situacao='Em Atraso'");
$arrayClientes = array();
while($linha = mysqli_fetch_array($listagem)){
$cliente = new Cliente(
$linha['nome'],
$linha['codigo'],
$linha['endereco'],
$linha['cpf'],
$linha['saldo'],
$linha['situacao'],
$linha['data']);
array_push($arrayClientes, $cliente);
}
return $arrayClientes;
}
}
$repositorio = new RepositorioClientesMySQL();
?>
You need to better formulate your question, don’t put everything in the question, be objective and explain what you need in the body of the question.
– touchmx
When deleting remember to put one
WHERE
with the client code.– rray
@geysa your code
$repositorio->excluirClientes($_REQUEST['codigo'])
is just a method, we need the content ofrepositorio_clientes.php
to know how this method works.– Guilherme Nascimento
insert the repositorio_clients page code.php
– Geysa
I don’t know if it has anything to do with it, but when I was correcting the code quote I saw the opening
<?php
was with space, like this< ?php
. I fixed this to format the code appear right (colored and such), but now I do not know if it was the case, because it could be a problem of the code right...– gustavox
that’s not it I put space in the code to post because I wasn’t just picking up as space anyway, in my work there’s no space ^^
– Geysa