0
In one of the files I have a dynamic table that receives data from BD
, but with the possibility of inserting more rows in the table. Note that when inserting more rows the idFaturacao
will be empty.
This is my table:
<TABLE id="dataTable">
<?php
$query_periodosFaturacao = mysqli_query($link,"SELECT * FROM faturacao WHERE fk_obras=$id ORDER BY num_fatura ASC");
while($periodosFaturacao = mysqli_fetch_array($query_periodosFaturacao)) {
echo "<TR>";
echo "<TD width='100px;'><INPUT type='date' id='data_afaturar' name='data_afaturar[]' value='".$periodosFaturacao['data']."' required/></TD>";
echo "<TD><input type='text' name='valor_afaturar[]' id='valor_afaturar' value='".$periodosFaturacao['valor']."' required>€</TD>";
echo "<input type='hidden' name='idFaturacao[]' value='".$periodosFaturacao['id']."' />";
echo "</TR>";
}
?>
</TABLE>
In another file where I send the data to the database, the problem is here! array
, the elements that are empty it makes the INSERT
and the elements in which idFaturacao
is filled it does the UPDATE
info. I don’t know how to check the empty and filled elements in the array.
$data_afaturar = $_POST['data_afaturar'];
$valor_afaturar = $_POST['valor_afaturar'];
$idFaturacao = $_POST['idFaturacao'];
foreach($data_afaturar as $a => $b){
if(empty($idFaturacao[$a])){
echo "INSERT";
}
else{
echo "UPDATE";
}
}
Thank you for your attention!
No time you declared the variable
$id_faturacao
, that seems to be wrong.– Francisco
Francisco the variable was declared here: $idFaturacao = $_POST['idFaturacao'];
– Joana Aguiar
No, it wasn’t. They’re different names, variable names should be strictly equal. See more here.
– Francisco
Francisco, you’re right had an underscore the most. Anyway does not correct the situation.
– Joana Aguiar
You have a variable $idFaturation and within the foreach you have another variable $idbilling with the minute f
– Marcelo Diniz
Your code is very meaningless, in the foreach you iterate on the
$data_afaturar
and in checking, you’re checking the$idFaturação
.– Francisco
Francisco in the foreach I have the $data_afaturar because this is an element that is never empty, so I want to check is when the $idFaturation is!
– Joana Aguiar
Got it. And php is claiming some error?
– Francisco
How are the fields that will be used to insert new data in HTML? In the question you put only the lines that are generated for the existing records. And as Francis asked, does any error appear? Which?
– Woss
Hello no error appears. just don’t do what I need you to do. That is the array has several elements, but some are empty. What I need is that it goes through the array and inside this one, the elements that have empty it do 'INSERT' and in the array the positions that have data it does 'UPDATE'. I don’t know if I made myself clear... For example, if the array looks like :'1', '2', '', '4'. It does the UPDATE of '1', '2' and '4', and in what is empty 'it does an INSERT
– Joana Aguiar
Joana, and how is the HTML of this field that comes with the
id
empty? Whereas what you posted in the question is just one loop of the bank records, there will always be aid
associated. Is there any other form that the user can enter new data?– Woss