PHP com formulario em LOOP

Enviada por Gustavo Balielo de Carvalho 
Gustavo Balielo de Carvalho
PHP com formulario em LOOP
27 de October de 2010 às 08:42PM
Pessoal to com um problema que pra mim parece grande...

Estou desenvolvendo um sistema que ira gerar pra mim um pedido de material!
Toda a parte de gerar o pedido eu ja implementei porem estou com dificuldade na alteração de um pedido gerado...
Exemplo... tenho no BD 3 tabelas a qual irá sofrer alteração, criei um form em loop que coloca o conteudo essas tabelas na tela pra mim em campos text ... até ai blz...
o problema vem na hr de alterar o conteudo do campo text.... quanto eu altero o cnteudo e clico em ALTERAR ele chama uma outra pagina php mais naum passa o conteudo do campo name"" do form.... ou melhor... ele só passa o conteudo da ultima linha!
deem uma olhada no código!
<html><head>
<script LANGUAGE="JavaScript">
function Botao1()
{
document.form1.action="teste.php";
document.forms.form1.submit();
}
</script></head>
<body>

<div class="container">
<div class="header" align="center"><!-- end .header -->
<p>Sistema de Gerenciamento Logístico - SisGLog<br>
APIC 2010</div>
<div class="content">
<p>Solicitação de Material nº
<?php
$consulta = $_POST['consulta'];
echo $consulta;
?></p>



<?php
echo "<form id='form1' name='form1' method='post' action='teste.php' >";
include 'conexao.php';
echo "<table width='' border='1' align='center'>
<tr>
<th width='264' align='center'>ITEM</td>
<td width='264' align='center'>CÓDIGO</td>
<td width='264' align='center'>DESCRIÇÃO</td>
<td width='264' align='center'>MARCA</td>
<td width='264' align='center'>UND</td>
<td width='264' align='center'>QTD</td>
<td width='264' align='center'>VALOR UNIT</td>
</tr>"
;
$sql = "SELECT MAX(item_sm) FROM item_sm_fnl where cod_sm = '$consulta';";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
while ($row[0] >= '1'){
$sql = "select i.item_sm, p.cod_prod, p.desc_prod, i.marca, p.und_prod, i.qtd, i.vlr_und
from item_sm_fnl i, produto p
where i.cod_sm='$consulta' and i.cod_prod = p.cod_prod and item_sm = '$row[0]'
;";
$query = mysql_query($sql);
$im = mysql_fetch_array($query);
echo "
<tr>
<th width='264'><input type='text' size='1' name='item' value='$im[0]'></th>
<td width='264'><input type='text' size='15' name='cod' value='$im[1]'></td>
<td width='264'><input type='text' size='30' name='desc' value='$im[2]'></td>
<td width='264'><input type='text' size='10' name='marca' value='$im[3]'></td>
<td width='264'><input type='text' size='1' name='und' value='$im[4]'></td>
<td width='264'><input type='text' size='1' name='qtd' value='$im[5]'></td>
<td width='264'><input type='text' size='5' name='vlr' value='$im[6]'></td>
<td width='264'><input type=button onclick=Botao1() value='Alterar'></td>
</tr>
";
$row[0] = $row[0] - 1;
}
echo "</table></form>";

?>


quando eu executo essa pagina ele gera uma tabela com um linha para cada registro de item na tabela item_sm_fnl juntando mais o conteudo de outra tabela, porem ele carrega cada registro em um campo TEXT DE UM FORM e para cada linha tem o botao ALTERAR que chama a função botao que esta na <head>

fica +/- assim

ITEM | CÓDIGO | DESCRIÇÃO | MARCA | UNIDADE | QUANTIDADE | VALOR
03 | 002 | XXX | XXX | Pç | 5 | 10,6 BOTÃO ALTERAR
02 | 004 | YYY | YYY | L | 6 | 20,8 BOTÃO ALTERAR
01 | 005 | ZZZ | ZZ | L | 6 | 66,8 BOTÃO ALTERAR

CADA VALOR EM UM CAMPO TEXT PARA QUE EU POSSA ALTERAR....

DAI POR EXEMPLO.... EU CLICO NO BOTAO ALTERAR DO ITEM 01 ELE PASSA TODO O CONTEUDO DOS CAMPOS name"" PARA A PAGINA SEGUINTE, DAI CONSIGO PEGAR PELO POST ($item = $_POST['item'];)

POREM SE EU CLICAR NO BOTÃO ALTERAR DO ITEM 03 OU 02 ELE PASSA O CONTEUDO SEMPRE DO ITEM 01....

ALGUEM SABERIA ME INDICAR UMA SOLUÇÃO!?

Grato!
Lucas Schirm Caixeta
Re: PHP com formulario em LOOP
19 de November de 2010 às 12:22PM
Você tem que colocar o name com colchete segue:

echo "
<tr>
<th width='264'><input type='text' size='1' name='item[]' value='$im[0]'></th>
<td width='264'><input type='text' size='15' name='cod[]' value='$im[1]'></td>
<td width='264'><input type='text' size='30' name='desc[]' value='$im[2]'></td>
<td width='264'><input type='text' size='10' name='marca[]' value='$im[3]'></td>
<td width='264'><input type='text' size='1' name='und[]' value='$im[4]'></td>
<td width='264'><input type='text' size='1' name='qtd[]' value='$im[5]'></td>
<td width='264'><input type='text' size='5' name='vlr[]' value='$im[6]'></td>
<td width='264'><input type=button onclick=Botao1() value='Alterar'></td>
</tr>
";


e no alterar é bom saber qual linha vc esta alterar par aevitar multiplos comandos inúteis.
Você precisa estar logado no PHPBrasil.com para poder enviar mensagens para os nossos fóruns.

Faça o login aqui.