Delete only one record in the database

Asked

Viewed 389 times

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.

  • When deleting remember to put one WHERE with the client code.

  • 2

    @geysa your code $repositorio->excluirClientes($_REQUEST['codigo']) is just a method, we need the content of repositorio_clientes.php to know how this method works.

  • insert the repositorio_clients page code.php

  • 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...

  • 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 ^^

Show 1 more comment

1 answer

0


If you just want to delete a client enter the id/code you don’t need a while for that. It is not necessary to use php to check which client of the list(array) of the list will be removed, just pass the id to the function let the database take care of this task.

Can simplify:

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

To:

if ($repositorio->excluirClientes($_REQUEST['codigo'])) {
   echo 'cliente excluido com sucesso';
} else {
   echo 'erro';
}
  • I made the change, more of the mistake by excluding.

  • Sorry had no error missed updated page...

  • but continues to exclude all registered customers

  • All customers have the same id?

  • 1

    yes, is this the problem? if this is how do I change the id?

  • In your table, if the database is mysql, in the id/code field set it to Primary key and auto increment.

  • 1

    Thanks for the help, now you’re deleting one at a time!

  • I have another question, now in the list of customers.

  • @geysa, I believe it is better to create a new question describing the problem, you can cite the link of this question if it is related.

  • Okay! I’ll do it.

  • @If this answer solved your problem, mark it as correct. If you don’t understand how Stackoverflow works, take a tour quickly: http://answall.com/tour

Show 6 more comments

Browser other questions tagged

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