Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/297085-function-update-db-getting-the-wrong-id/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.