I'm having an issue in my code when I try to put multiple entries. I tried different solutions, but the problem was not solved. The only problem is to get the value of the field 'valor'
<? $produtocotacao = "select * from cadcotacao2 WHERE idcotacao = '$idcotacao' ORDER by id DESC";
$result = mysql_query ($produtocotacao, $marechal) or die(mysql_error());
while ($linha = mysql_fetch_array ($result)) {
$id = $linha["id"];
$idproduto = $linha["idproduto"];
$produto = $linha["produto"];
$user = $linha["user"];
?>
<form id="form6" method="post" action=""><tr>
<td align="center"><? echo $linha['idproduto']; ?></td>
<td align="center"><? echo $linha['produto']; ?></td>
<td align="left" valign="middle">
<input name="id6[]" type="hidden" value="<? echo $linha['id']; ?>" />
<input name="idcotacao[]" type="hidden" value="<? echo $idcotacao; ?>" />
<input name="idproduto[]" type="hidden" value="<? echo $linha['idproduto']; ?>" />
<input name="idfornecedor[]" type="hidden" value="<? echo "teste"; ?>" />
<input name="ip[]" type="hidden" value="<? echo $ip; ?>" />
<input name="valor[]" type="text" id="valor" placeholder="Ex: 110,50" class="m-wrap span6" />
</td>
</tr>
<?
if(isset($_POST['done2'])){
$cotacao = array();
$cotacao[] = array('id' => $linha['id'], 'idcotacao' => $idcotacao, 'idproduto' => $idproduto, 'idfornecedor' => "teste", 'ip' => $ip, 'valor' => $_POST['valor']);
foreach ($cotacao as $rst) {
$id = $rst['id'];
$idcotacao = $rst['idcotacao'];
$idproduto = $rst['idproduto'];
$idfornecedor = $rst['idfornecedor'];
$ip = $rst['ip'];
$valor = $rst['valor'];
$sql = "INSERT INTO `cadcotacao4` (`ip`, `idcotacao`, `idproduto`, `idfornecedor`, `valor`) VALUES ('{$ip}', '{$idcotacao}', '{$idproduto}', '{$idfornecedor}', '{$valor}');";
if(mysql_query($sql)){
$erro = "Cotação Respondida!";
}
}
}
} ?>
The code is recording in mysql these lines below:
id timestamp ip idforn idprod forn valor
47 2016-08-25 15:47:45 179.209.99.217 2 6745 teste Array
48 2016-08-25 15:47:45 179.209.99.217 2 1 teste Array
The difficulty with your question is that your code seems very flawed structurally. Forgive me if I'm misunderstanding what you are trying to do.
It seems you are making the classic PHP novice mistake of not realising that ALL PHP code on a page is executed (on the server) before any dynamic HTML and/or JavaScript etc. is executed on the client end.
So, I would tend to put any server end processing at the start of your script - to remind you that it will get done first - putting POSTed form/results data processing first of all, followed with preparation of data to display on your new page.
Secondly if you are using multiple type inputs(valor[]), it only really make sense if you are repeating the inputs within one form - so why are you redeclaring the form for every row? Only ONE form will be POSTed.
Either use one form with repeating multiple inputs (if you want to change multiple rows with one submit) OR use separate forms (preferably differently named) with simple inputs (valor)(to display multiple results but only change one at a time) instead of multiples (valor[]).
So, I would rearrange your code something like this:
<?php
if(isset($_POST['done2'])){
$ids=$_POST['id6'];
$idcotacaos = $_POST['idcotacao'];
$idprodutos = $_POST['idproduto'];
$idfornecedors = $_POST['idfornecedor'];
$ips = $_POST['ip'];
$valors = $_POST['valor'];
$i=0;
foreach($ids as $id)
{
$idcotacao = $idcotacaos[$i];
$idproduto = $idprodutos[$i];
$idfornecedor = $idfornecedors[$i];
$ip = $ips[$i];
$valor = $valors[$i];
$sql = "INSERT INTO `cadcotacao4` (`ip`, `idcotacao`, `idproduto`, `idfornecedor`, `valor`) VALUES ('{$ip}', '{$idcotacao}', '{$idproduto}', '{$idfornecedor}', '{$valor}');";
if(mysql_query($sql)){
$erro = "Cotação Respondida!";
}
$i++;
}
}
$produtocotacao = "select * from cadcotacao2 WHERE idcotacao = '$idcotacao' ORDER by id DESC";
$result = mysql_query ($produtocotacao, $marechal) or die(mysql_error());
?>
<form id="form6" method="post" action="">
<?php
while ($linha = mysql_fetch_array ($result)) {
$id = $linha["id"];
$idproduto = $linha["idproduto"];
$produto = $linha["produto"];
$user = $linha["user"];
?>
<tr>
<td align="center"><? echo $linha['idproduto']; ?></td>
<td align="center"><? echo $linha['produto']; ?></td>
<td align="left" valign="middle">
<input name="id6[]" type="hidden" value="<? echo $linha['id']; ?>" />
<input name="idcotacao[]" type="hidden" value="<? echo $idcotacao; ?>" />
<input name="idproduto[]" type="hidden" value="<? echo $linha['idproduto']; ?>" />
<input name="idfornecedor[]" type="hidden" value="<? echo "teste"; ?>" />
<input name="ip[]" type="hidden" value="<? echo $ip; ?>" />
<input name="valor[]" type="text" id="valor" placeholder="Ex: 110,50" class="m-wrap span6" />
<input type="submit" name="done2" value="Save Changes">
</td>
</tr>
<?php
}
?>
</form>
Bit of a change looping through the ids and getting the elements of each of the other multiple[] POST values within that loop then executing the sql using those elements. Hoping that's the kind of thing you were trying to achieve.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.