Olá !
Tenho uma página que faz uma busca na db mysql a partir do que foi digitado no campo de busca.
Ele me retorna somente com um registro.
Então nesta mesma página tenho um campo e botão que alteram um dado desse registro . Para funcionar no resultado da busca , utilizei o $id como referência. Mas ele está indo sempre para o id errado (sempre o primeiro id da tabela) , ao inves de alterar o registro encontrado na busca. O que tem de errado ?
Segue código:
<?php include "config.php"; ?> <?php mysql_set_charset('utf8'); ini_set('default_charset','UTF-8'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Resultado Busca</title> </head> <body> <a href="/admin/painel/buscar.php"> <img src="/img/back.png" /></a ><br/> <br/> <br/> <?php $buscar=$_POST['buscar']; $sql = mysql_query("SELECT * FROM trocasrecebidas WHERE correios LIKE '%".$buscar."%'"); $row = mysql_num_rows($sql); if ($row > 0) { while($linha = mysql_fetch_array($sql)) { $id = $linha['id']; $nome = $linha['nome']; $email=$linha['email']; $telefone=$linha['telefone']; $pedido=$linha['pedido']; $correios=$linha['correios']; $endereco=$linha['endereco']; $cep=$linha['cep']; $cidade=$linha['cidade']; $estado=$linha['estado']; $motivodefeito=$linha['motivodefeito']; $motivoerrado=$linha['motivoerrado']; $motivodesistiu=$linha['motivodesistiu']; echo "<strong>ID: </strong>".@$id; echo "<br/><br/>"; echo "<strong>Nome: </strong>".@$nome; echo "<br/><br/>"; echo "<strong>Email: </strong>".@$email; echo "<br/><br/>"; echo "<strong>Telefone: </strong>".@$telefone; echo "<br/><br/>"; echo "<br/><br/>";echo "<br/>"; echo "<strong>N°do pedido: </strong>".@$pedido; echo "<br/><br/>"; echo "<strong>Cód. de rastreamento: </strong>".@$correios; echo "<br/><br/>"; echo "<strong>Produto com defeito: </strong>".@$motivodefeito; echo "<br/>"; echo "<strong>Produto errado: </strong>".@$motivoerrado; echo "<br/>"; echo "<strong>Desistência: </strong>".@$motivodesistiu; echo "<br/>"; echo "<br/><br/>";echo "<br/>"; echo "<strong>Endereço: </strong>".@$endereco; echo "<br/><br/>"; echo "<strong>CEP: </strong>".@$cep; echo "<br/><br/>"; echo "<strong>Cidade: </strong>".@$cidade; echo "<br/><br/>"; echo "<strong>Estado: </strong>".@$estado; echo "<br/><br/>";echo "<br/>";echo "<br/><br/>"; } } else { echo "Nenhum registro encontrado."; } ?> <p> <p> <p> <p> <form name="button" method="post" action=""> <input type="submit" value="Imprimir" /> </form> <br/><br/> <form name="searchform" method="post" action="?a=ok"> <input type="text" name="newrastreio" /> <input type="submit" value="Novo Rastreamento" /> </form> <?php if ( isset( $_GET['a'] ) && $_GET['a'] == 'ok' ) { $newrastreio=$_POST['newrastreio']; $alterarDado = mysql_query("UPDATE trocasrecebidas SET newrastreio = '".$newrastreio."' WHERE id = '".@$id."'") or die (mysql_error()); } ?> </body> </html>
No caso , a parte do problema é:
<form name="searchform" method="post" action="?a=ok"> <input type="text" name="newrastreio" /> <input type="submit" value="Novo Rastreamento" /> </form> <?php if ( isset( $_GET['a'] ) && $_GET['a'] == 'ok' ) { $newrastreio=$_POST['newrastreio']; $alterarDado = mysql_query("UPDATE trocasrecebidas SET newrastreio = '".$newrastreio."' WHERE id = '".@$id."'") or die (mysql_error()); } ?>
Obrigado.