GustavoLopesC Posted June 28, 2015 Share Posted June 28, 2015 Hello , I have one page that makes a search inside the DB MYSQL about whats was wrote in the search field. And returns me a registry. In this same page i have one field and a button that change one data of this registry. For make this work in the registry found in the search , i used the $id like reference. But it's going to the wrong $id in the DB (only change the first id on the table) , instead of change the registry that i found. What's wrong ? Code: <?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> The part of the problem: <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()); } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/297085-function-update-db-getting-the-wrong-id/ Share on other sites More sharing options...
fastsol Posted June 29, 2015 Share Posted June 29, 2015 $id will always only be the value of the last row the while loop gathers cause it's being reset with each loop of the while. Quote Link to comment https://forums.phpfreaks.com/topic/297085-function-update-db-getting-the-wrong-id/#findComment-1515178 Share on other sites More sharing options...
GustavoLopesC Posted June 29, 2015 Author Share Posted June 29, 2015 $id will always only be the value of the last row the while loop gathers cause it's being reset with each loop of the while. And what can i do ? Quote Link to comment https://forums.phpfreaks.com/topic/297085-function-update-db-getting-the-wrong-id/#findComment-1515181 Share on other sites More sharing options...
Ch0cu3r Posted June 30, 2015 Share Posted June 30, 2015 You will need to output the form within the while loop and set the id as a hidden input field. while($linha = mysql_fetch_array($sql)) { $id = $linha['id']; .... echo '<form name="searchform" method="post" action="?a=ok"> <input type="text" name="newrastreio" /> <input type="hidden" name="id" value="' . $id . '" /> <input type="submit" value="Novo Rastreamento" /> </form>'; } When updating the record you get the id value from $_POST['id'] Quote Link to comment https://forums.phpfreaks.com/topic/297085-function-update-db-getting-the-wrong-id/#findComment-1515260 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.