1
I have the following problem I want to do a search in my database and display the results in the list lines, only it is giving a crazy error, it re-displays the entire page three times, gets like oh:
Someone can help me?
have any idea why this happens?
could help me with a more efficient code for the search?
Below follows the complete code:
$servidor = "localhost";
$usuario = "root";
$senha = "";
$banco = "test";
$conexao = mysql_connect($servidor, $usuario, $senha)or die("Erro: " . mysql_error());
mysql_select_db($banco,$conexao)or die("Erro: " . mysql_error());
$query =("SELECT cod_cliente, nome, cpf, endereco, celular FROM clientes");
// executa a query
$dados = mysql_query($query) or die(mysql_error());
// transforma os dados em um array
$linha = mysql_fetch_assoc($dados);
// calcula quantos dados retornaram
$total = mysql_num_rows($dados);
?>
// se o número de resultados for maior que zero, mostra os dados
if($total > 0) {
// inicia o loop que vai mostrar todos os dados
do {
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8"/>
<meta charset="ISO 8859-1"/>
<link rel="stylesheet" type="text/css" href="nocsys.css">
<title>Telefonia - Consultar Cliente</title>
</head>
<body>
<div class="containermenu">
php include 'menu.php';?>
</div>
<div class="container">
<form id="contact">
<h3>Pesquisar Cliente</h3>
<h4>Por Favor informe os dados de forma correta!</h4>
<fieldset>
<input name="consulta" id="consulta" maxlength="150" placeholder="nome ou cpf" type="text" tabindex="1" required autofocus>
</fieldset>
<fieldset>
<button name="submit" id="submit" type="submit" id="contact-submit" data-submit="...Sending" action="pesquisarcliente.php" method="get">Consultar</button>
</fieldset>
<div class="containermenu">
<ul>
<li>
<a >Cod Cliente</a>
</li>
<li>
<a>Nome</a>
</li>
<li>
<a>CPF/CNPJ</a>
</li>
<li>
<a>Celular</a>
</li>
<li>
<BR>
<a>Endereço</a>
</li>
</ul>
<ul>
<li>
<a><a>?=$linha['cod_cliente']? = nome vindo do banco após a pesquisa</a></a>
</li>
<li>
<a><a>?=$linha['nome']?</a></a>
</li>
<li>
<a><a>?=$linha['cpf']?</a></a>
</li>
<li>
<a><a>?=$linha['celular']?</a></a>
</li>
<BR>
<li>
<a><a>?=$linha['endereco']?</a></a>
</li>
</ul>
</div>
// finaliza o loop que vai mostrar os dados
}while($linha = mysql_fetch_assoc($dados));
// fim do if
}
?>
// tira o resultado da busca da memória
mysql_free_result($dados);
?>
<BR>
<fieldset>
<button name="submit" id="submit" type="submit" id="contact-submit" data-submit="...Sending">Alterar</button>
</fieldset>
<BR>
<div class="container">
<div class="row">
<div class="col-md-10">
<ul class="pagination">
<li>
<a href="#">Prev</a>
</li>
<li>
<a href="#">1</a>
</li>
<li>
<a href="#">2</a>
</li>
<li>
<a href="#">3</a>
</li>
<li>
<a href="#">4</a>
</li>
<li>
<a href="#">5</a>
</li>
<li>
<a href="#">Next</a>
</li>
</ul>
</div>
</div>
</div>
<fieldset>
<button name="submit" id="submit" type="submit" id="contact-submit" data-submit="...Sending">Apagar</button>
</fieldset>
</form>
</div>
</body>
</html>
// tira o resultado da busca da memória
mysql_free_result($dados);
?>
It’s missing the tags to be able to publish it here, otherwise it doesn’t show, but in my code it’s all right
– tbmf89
So, I think the point is that you’re creating the page in if. inside if should only contain the part that Oce wants you to repeat. I take this opportunity to tell you to use PDO, because mysql_query is obsolete. I will create another template here and change the answer now
– Italo Rodrigo
take a look now
– Italo Rodrigo
it is not returning anything... but for it to respond as PDO has to make a configuration in the Xampp file has no?
– tbmf89
I do not think you need to change the xamp. use here in wamp good. gives some error or just does not return anything? after the line
$pdo = new PDO('mysql:host='.$host.';dbname='.$dbname,$user,$pass);
usevar_dump($pdo);
and see if anything returns.– Italo Rodrigo
Returns this Object(PDO)#1 (0) { }
– tbmf89
I changed the code again here and this time I tested it and everything is working perfectly. if you have an error, it will be the connection variables or select
– Italo Rodrigo
what’s changed? I couldn’t see... it feels the same
– tbmf89
just a matter of commas and added php after <?
– Italo Rodrigo
you tested? tell me the result
– Italo Rodrigo
Notice: Trying to get Property of non-object in C: xampp htdocs Projetonocsystest consultarcliente.php on line 61
– tbmf89
it’s strange the two codes above have the same connection and one of the two not work :/
– Italo Rodrigo
Structure of my bank TABLE clients(cod_client INT(11)PRIMARY KEY NOT NULL AUTO_INCREMENT, name VARCHAR(150) NOT NULL, Cpf VARCHAR(14) UNIQUE NOT NULL, address VARCHAR(254) NOT NULL, cellular VARCHAR(11)NOT NULL, email VARCHAR(75)NOT NULL); ENGINE=Innodb DEFAULT CHARSET=latin1 AUTO_INCREMENT=2;
– tbmf89
It is no problem in the database, because you were able to connect with the code of my previous answer. check if there is data in the database. and try to create a new page based on the code that worked.
– Italo Rodrigo