Quantcast
Viewing all articles
Browse latest Browse all 14700

modificar um registo num banco de dados mysql

Boa tarde caros amigos.
Estou a tentar modificar um registo de um banco de dados, através de um formulário e de uma página em php. No entanto, apesar da existência de ligação entre o formulário e a página, através do campo auto-incrementado «id» e de os dados estarem visiveis nos campos do formulário, quando é executada a query para o update, todos os campos perdem os seus valores.
Resulta daqui uma modificação, mas sem introdução dos dados alterados.

O código do formulário é o seguinte:
<?php require_once('funcoes.php');
conexao();

ini_set('register_globais',true);
$tbl_name="tascas";

$sql="SELECT * FROM $tbl_name where id=".$_GET['id'];
$result=mysql_query($sql);

  while($rows = mysql_fetch_array($result)) {
$rowsid = $rows['id'];
     $rowsnome = $rows['nome'];
$rowsendereco = $rows['endereco'];
     $rowslocal = $rows['local'];
     $rowsconcelho = $rows['concelho'];
     $rowslocalizacao = $rows['localizacao'];
     $rowstewlefone = $rows['telefone'];
     $rowsprato = $rows['prato'];
     $rowsespecialidade = $rows['especialidade'];
     $rowsfolga = $rows['folga'];
     $rowsestacionamento = $rows['estacionamento'];
     $rowsregiao = $rows['regiao'];
     $rowsobservacoes = $rows['observacoes'];    
  }
?>
<body bgcolor="#00E5EE">
<table with= "400" border="0" align="center" cellpadding="3" cellspacing="0" >
<tr>
<td> <strong>Moficar a Ficha do Restaurante</strong></td>
</tr>
</table>
<table with= "400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FF3300" >
<tr>
<form   action="mudar.php" id="form1" name="form1" method="post" >
<td>
<table with= "400" border="0" cellpadding="3" cellspacing="1" bgcolor="#00FF00" >
<tr>
<td width="117">Nome do restaurante</td>
<td width="14"> : </td>
<td width="357"> <input name="nome" type="text" id="nome1" size="40" value="<?php echo $rowsnome ?>" /></td>
</tr>
<tr>
<td width="117">Endereço</td>
<td width="14"> : </td>
<td width="357"> <input name="endereco" type="text" id="endereco1" size="150" value="<?php echo $rowsendereco ?>" /></td>
</tr>
<tr>
<td width="117">Local</td>
<td width="14"> : </td>
<td width="357"> <input name="local" type="text" id="local1" size="40" value="<?php echo $rowslocal ?>"/></td>
</tr>
<tr>
<td width="117">Concelho</td>
<td width="14"> : </td>
<td width="357"> <input name="concelho" type="text" id="concelho1" size="40" value="<?php echo $rowsconcelho ?>"/></td>
</tr>
<tr>
<td width="117">Localização</td>
<td width="14"> : </td>
<td width="357"> <input name="localizacao" type="text" id="localizacao1" size="150" value="<?php echo $rowslocalizacao ?>"/></td>
</tr>
<tr>
<td width="117">Telefone</td>
<td width="14"> : </td>
<td width="357"> <input name="telefone" type="text" id="telefone1" size="40" value="<?php echo $rowstelefone ?>"/></td>
</tr>
<tr>
<td width="117">Prato</td>
<td width="14"> : </td>
<td width="357"> <input name="prato" type="text" id="prato1" size="40" value="<?php echo $rowsprato ?>"/></td>
</tr>
<tr>
<td width="117">Especialidade</td>
<td width="14"> : </td>
<td width="357"> <input name="especialidade" type="text" id="especialidade1" size="150" value="<?php echo $rowsespecialidade ?>"/></td>
</tr>
<tr>
<td width="117">Dia de Folga</td>
<td width="14"> : </td>
<td width="357"> <input name="folga" type="text" id="folga1" size="40" value="<?php echo $rowsfolga ?>"/></td>
</tr>
<tr>
<td width="117">Estacionamento</td>
<td width="14"> : </td>
<td width="357"> <input name="estacionamento" type="text" id="estacionamento1" size="40" value="<?php echo $rowsestacionamento ?>"/></td>
</tr>
<tr>
<td width="117">Região</td>
<td width="14"> : </td>
<td width="357"> <input name="regiao" type="text" id="regiao1" size="40" value="<?php echo $rowsregiao ?>"/></td>
</tr>
<tr>
<td valign="top">Observações</td>
<td valign="top"> : </td>
<td><textarea class="observacoes1" name="observacoes" cols="100" rows="10" id="observacoes1" ><?php echo $rowsobservacoes ?></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table with= "400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="mudar.php?id=<?php echo $rowsid; ?>">Modificar Registo</a></strong></td>
</tr>
</table>

o Código da página que procede à alteração é este:
<?php
require_once('funcoes.php');
conexao();
$tbl_name="tascas";

ini_set('register_globais',true);

mysql_query("UPDATE $tbl_name SET nome='".$_POST["nome"]."', endereco='".$_POST["endereco"]."', local='".$_POST["local"]."', concelho='".$_POST["concelho"]."', localizacao='".$_POST["localizacao"]."', telefone='".$_POST["telefone"]."', prato='".$_POST["prato"]."', especialidade='".$_POST["especialidade"]."', folga='".$_POST["folga"]."', estacionamento='".$_POST["estacionamento"]."', regiao='".$_POST["regiao"]."', observacoes='".$_POST["observacoes"]."' where id=".$_GET["id"].";");

?>
<td><strong><a href="listarmuda.php">Listar Restaurantes</a></strong></td>
<?php

mysql_close();
?>

Acresce a tudo isto o facto de ser iniciante em php, pelo que espero alguma indulgência na apreciação da minha dúvida que deverá ser "básica". Já substituí o valor dos campos que passam os dados a variável resultante do GET, pela variável visivel no conteúdo do formulário (por exemplo substituí $_POST["nome"] por $_POST[$rowsnome]), mas com o mesmo resultado.
Antecipadamente grato pela vossa paciência... Image may be NSFW.
Clik here to view.
:)

Viewing all articles
Browse latest Browse all 14700

Trending Articles