0
I’m having trouble making an array foreach, is showing error, follow the code below:
<table style="width: 100%;">
<thead>
<tr>
<th>Item</th>
<th>Código</th>
<th>Produto</th>
<th>Valor</th>
</tr>
</thead>
<tbody>
<?php while($dado_produto = $result_produtos->fetch_array()){ ?>
<tr>
<td>1</td>
<td><?php echo $dado_produto['cod']; ?></td>
<td><?php echo $dado_produto['descricao']; ?></td>
<td><input type = "text" name="valor[<?php echo $dado_produto['cod']; ?>]"/>
<input type = "hidden" name="linha[<?php echo $dado_produto['linha']; ?>]"/>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<input type="submit"/>
</form>
The php value. receives the values by the POST method
<?php
header('Content-Type: text/html; charset=utf-8');
include_once("../../controle/conexao.php");
// Início da consulta
$sql = "INSERT INTO `produtos` (`cod`, `valor`, `linha`) VALUES";
// Para cada elemento produto:
foreach($_POST['valor'] as $cod=>$val; $_POST['linha'] as $linha){
// Monta a parte consulta de cada produto
$sql .= " ('{$produto}', '{$valor}', '{$linha}'),";}
// Tira o último caractere (vírgula extra)
$sql = substr($sql, 0, -1);
// Executa a consulta
mysqli_query($sql);
$cadastrados = mysqli_affected_rows();
?>
But you’re making me wrong again
PHP Parse error: syntax error, Unexpected ';', expecting ')'
The line that error occurs is:
foreach($_POST['value'] as $Cod=>$val; $_POST['line'] as $line){
I’ve switched to comma but still gives error, this code I took from a web site as reference, but apparently is incorrect. What would be the correct method?
Are you trying to do the
foreach
in two arrays at the same time? I believe that this is not possible in this way.– Woss
Check the correct way to make one foreach the way it is.... is generating error.
– Don't Panic