PHP Error Parse error: syntax error, Unexpected ';', expecting ')' when making an array foreach

Asked

Viewed 1,040 times

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.

  • Check the correct way to make one foreach the way it is.... is generating error.

1 answer

3


This syntax is totally wrong:

foreach($_POST['valor'] as $cod=>$val; $_POST['linha'] as $linha){

As the doc syntax is only this:

foreach (array_expression as $key => $value)
    statement

You don’t use two expressions at the same time, the ; unexpected is probably the inside of the foreach

Another problem is that your query has 4 values:

$sql .= " (1, '{$produto}', '{$valor}', '{$linha}'),";}

But this is only calling 3 columns in Insert:

$sql = "INSERT INTO `produtos` (`cod`, `valor`, `linha`) VALUES";

mysql will trigger an error like this

Solving the iteration

To be honest I couldn’t quite understand the meaning of the code, but I believe that if the goal is to insert a value for each product you can change the HTML to this:

<td><?php echo $dado_produto['cod']; ?></td>
<td><?php echo $dado_produto['descricao']; ?></td>
<td>

    <input type="hidden" name="novo_valor[]" value="" />
    <input type="hidden" name="cod[]" value="<?php echo $dado_produto['cod']; ?>" />
    <input type="hidden" name="linha[]" value="<?php echo $dado_produto['linha']; ?>"/>
</td>

And the for would look like this:

$sql = "INSERT INTO `produtos` (`cod`, `valor`, `linha`) VALUES";

$novos_valores = $_POST['novo_valor'];

// Para cada elemento produto:
for ($i = 0; $i < count($novos_valores); $i++){

    $novo_valor = $novos_valores[$i];
    $linha = $_POST['linha'][$i];
    $cod = $_POST['cod'][$i];

    // Monta a parte consulta de cada produto
    $sql .= " ('{$cod}', '{$novo_valor}', '{$linha}'),";
}

//IMPORTANTE: o mysql_query tem que ficar fora do `for`

// Tira o último caractere (vírgula extra)
$sql = substr($sql, 0, -1);

// Executa a consulta
mysqli_query($sql);
$cadastrados = mysqli_affected_rows();

note: in the way that this your code can suffer attacks from Sqlinjection recommend to exchange the approach for Prepared Statements

  • @Junior I’ll venture a guess then, but I really hope it’s just this, otherwise and it’s not I’ll close the question until you’ve explained the goal better (edit your question), don’t take it badly okay? Just wait a little while and I’ll edit my answer and you see if this resolves

  • I take no, that we find a lot of crap on the internet and hard to learn alone.. with so many bad things

  • Registering multiple records in Mysql at the same time, this is the goal of this code I took from http://blog.thiagobelem.net/cadastrantrando-multiplos-registraos-no-mysql-ao-mesmo-tempo, and another found on the imaster forum.

  • @Junior I edited again, see if you can understand.

  • 1

    thank you so much for the reply and the tip from Sqlinjection :)

  • see if you can help me, now if I want to update it is the same logic ?

  • Complementing. Be sure to check if the required fields have been informed. Often it may occur of an empty field etc

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.