-1
I am making a page with registration of product values, I would like to know how I do a Insert Into
of the values of a while?
<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']; ?>]"/></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<input type="submit"/>
</form>
The file that receives the php value.
I searched I used a code I found on the internet to, but I’m not getting, this giving error.
<?php
header('Content-Type: text/html; charset=utf-8');
include_once("../../controle/conexao.php");
// Início da consulta
$sql = "INSERT INTO `produtos` (`cod`, `valor`) VALUES";
// Para cada elemento produto:
foreach($_POST['valor'] as $cod=>$val){
$produto = $cod['codigo'];
$valor = $val['valores'];
// Monta a parte consulta de cada produto
$sql .= " (NULL, '{$produto}', '{$valor}'),";}
// Tira o último caractere (vírgula extra)
$sql = substr($sql, 0, -1);
// Executa a consulta
mysqli_query($sql);
$cadastrados = mysqli_affected_rows();
?>
Error appears:
PHP Parse error: syntax error, Unexpected T_VARIABLE in E: home value.php on line 10
Line 10 is the :
$sql .= " (NULL, '{$produto', '{$valor}'),";}
Can someone help me what I’m doing wrong?
Are we missing a comma but not a } ? $sql .= " (NULL, '{$product}', '{$value}')";}
– Sr. André Baill
I put } right after product and removed the comma, but still presents the same error, I returned the comma and also presents the same error.
– Junior
Looks like missing a semicolon:
$valor = $val['valores']
– rray
put, and now appears the error PHP Warning: mysqli_query() expects at least 2 Parameters, 1 Given in E: home value.php on line 11 PHP Warning: mysqli_affected_rows() expects Exactly 1 Parameter, 0 Given in E: home value.php on line 12
– Junior
The field you are setting NULL will not be set to "not null"? Try putting "1" instead of NULL
– lazyFox
When you use the functions
mysqli
the first argument is almost always the connection, the second the query in the case of themyqli_query()
. Look at youmysqli_query($sql);
– rray
@rray really the errors that André Baill and more what you pointed out, was one of the problems, deleted $product = $Cod['code']; $value = $val['values']; is now working correctly. Thanks for the help
– Junior