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:


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

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

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


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;?>" />

                <th><input type="button" value="+" id="add" class="btn btn-primary"></th>

            <tbody id="orderdetail" class="detail">
                while ($result = mysql_fetch_object($query)){
                    <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>
            <?php } ?>  
<input type="submit" class="btn btn-primary" name="update" id="update" value="Salvar">

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'.

@ini_set('display_errors', '1');

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

$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]

header("Location: consulta_orc.php");

Member Avatar

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, learning, and sharing knowledge.