Error duplicating HTML form in Mysql PHP Data Search

Asked

Viewed 141 times

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:inserir a descrição da imagem aqui

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);
?>

2 answers

0

redacted

<?php
$host   = 'localhost'; // endereço do servidor de banco de dados.
$dbname = 'test'; //nome de sua base de dados
$user   = 'root'; // seu usuário do banco
$pass   = ''; // sua senha do banco
$pdo    = new PDO('mysql:host='.$host.';dbname='.$dbname,$user,$pass);

$pegaUsuario = $pdo->prepare('SELECT cod_cliente, nome, cpf, endereco, celular FROM clientes');
$pegaUsuario->execute();
$usuario = $pegaUsuario->fetchAll(PDO::FETCH_OBJ);

?>
<!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><?php echo $usuario[0]->id; ?></a>
</li>
<li>
<a><?php echo $usuario[0]->nome; ?></a>
</li>
<li>
<a><?php echo $usuario[0]->cpf; ?></a>
</li>
<li>
<a><?php echo $usuario[0]->celular; ?></a>
</li>
<BR>
<li>
<a><?php echo $usuario[0]->endereco; ?></a>
</li>
</ul>
</div>

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

I changed the code again and tested it on my machine with a bank I have here. if there is something wrong with the connection, check the values you are using in the host variables, dbname, etc.

  • 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

  • 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

  • take a look now

  • it is not returning anything... but for it to respond as PDO has to make a configuration in the Xampp file has no?

  • 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); use var_dump($pdo); and see if anything returns.

  • Returns this Object(PDO)#1 (0) { }

  • 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

  • what’s changed? I couldn’t see... it feels the same

  • just a matter of commas and added php after <?

  • you tested? tell me the result

  • Notice: Trying to get Property of non-object in C: xampp htdocs Projetonocsystest consultarcliente.php on line 61

  • it’s strange the two codes above have the same connection and one of the two not work :/

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

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

Show 9 more comments

0

create any PHP file and write only the code below:

<?php
$host   = 'localhost'; // endereço do servidor de banco de dados.
$dbname = 'nome_do_banco'; //nome de sua base de dados
$user   = 'nome_do_usuairo'; // seu usuário do banco
$pass   = 'senha'; // sua senha do banco
$pdo    = new PDO('mysql:host='.$host.';dbname='.$dbname,$user,$pass);

$pegaUsuario = $pdo->prepare('SELECT cod_cliente, nome, cpf, endereco, celular FROM clientes');
$pegaUsuario->execute();
$usuario = $pegaUsuario->fetchAll(PDO::FETCH_OBJ);

echo 'Bem vindo ' . $usuario[0]->nome;
?>

check whether the data $host, $dbname, $user and $pass are correct.

test and see if connects.

if it works, just adapt to the code above.

  • Now Return Welcome Thyago Breder Marquy Furtado

  • use this code here since it worked, and gradually adding your html tags and testing. in case of error in a specific part of the code, it is easier to find

  • Insert I already did and such, but the search was not working, helps me then to do the crud without the PDO, only the same search, and a Delete, I need to finish this soon and to get stuck in this crap...

  • to do without the PDO, simply exchange the connection commands for the ones you were previously using

Browser other questions tagged

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