Hello, everybody!

I have learned a lot by reading others questions and your solutions. Now it's time to ask one myself.

I have a dynamic form (with dynamically adding rows).
This form has 2 normal input fields which send data to table 'orcamen'
and 4 dynamic fields which are stored in table 'tbl_orderdetail'.
I have already achieved to retrieve the stored data from both tables when I click the edit button.
Also, both normal fields are udating values in table 'orcamen'.

My problem - How can I update the dynamic fields?

This is my edit page that fetches correctly the values from both tables:

<?php 
mysql_connect("localhost","root");
mysql_select_db("alpha");

$id = $_GET["id"];
settype($id, "integer");

    $resultado=mysql_query("SELECT * FROM orcamen WHERE id = $id");
    $orcrow=mysql_fetch_object($resultado); 

//This is the table that gets dynamic fields    
$query = mysql_query( "SELECT * FROM tbl_orderdetail WHERE  order_id=$id");

mysql_close();
?>

And here is the html code of the dynamic rows:

<form   method="post" action="salva_orc.php">   
<input type="hidden" name="id" id="id" value="<?php echo $id;?>" />

<thead>
                <th>No</th>
                <th>Qtde</th>
                <th>Descrição</th>
                <th>Unitário</th>
                <th>Desc.(%)</th>
                <th>Valor</th>
                <th><input type="button" value="+" id="add" class="btn btn-primary"></th>
            </thead>


            <tbody id="orderdetail" class="detail">
            <?php
                while ($result = mysql_fetch_object($query)){
            ?>
                <tr>
                    <td width="2%" class="no">1</td>
                    <td width="10%"><input type="text" class="form-control quantity" name="quantity[]" value="<?php echo $result->quantity ?>"></td>
                    <td width="60%"><input type="text" class="form-control productname" name="productname[]" value="<?php echo $result->product_name ?>"></td>
                    <td width="8%"><input type="text" class="form-control price" name="price[]" value="<?php echo $result->price ?>"></td>
                    <td width="4%"><input type="text" class="form-control discount" name="discount[]" value="<?php echo $result->discount ?>"></td>
                    <td width="10%"><input type="text" class="form-control amount" name="amount[]" value="<?php echo $result->amount ?>"></td>
                    <td width="6%"><a href="#" class="remove">Excluir</td>
                </tr>
            <?php } ?>  
            </tbody>   
<input type="submit" class="btn btn-primary" name="update" id="update" value="Salvar">
</form> 

And now the update file that is called from the upper form. The first part is updating correctly, but I have no clue on how to update the dynamic fields into table 'tbl_orderdetail'.

<?php 
@ini_set('display_errors', '1');
error_reporting(E_ALL);

mysql_connect("localhost", "root", "");
mysql_select_db("alpha");


$razao     = $_POST["razao"];
$local      = $_POST["local"];
$condicao     = $_POST["condicao"];
$estado         = $_POST["estado"];
$material       = $_POST["material"];
$obs        = $_POST["obs"];
$id         = $_POST["id"];
mysql_query ("UPDATE orcamen SET razao='$razao' , local='$local' , condicao='$condicao' , estado='$estado' , material='$material' , obs='$obs' WHERE id=$id");

$id = mysql_insert_id();
for($i = 0 ;$i < count($_POST['productname']);$i++)
{
    mysql_query("UPDATE tbl_orderdetail 
                SET order_id        = $id, 
                    product_name    = ['product_name'][$i], 
                    quantity        = ['quantity'][$i], 
                    price           = ['price'][$i], 
                    discount        = ['discount'][$i], 
                    amount          = ['amount'][$i]
                ");
}

mysql_close();
header("Location: consulta_orc.php");

?>    
Member Avatar for diafol

Before anything else, are you going to stick with deprecated mysql_* functions or are you going to update with either mysqli or PDO?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.