I have 2 nested tables. Parent table has size field and child table is in row of parent table that consists color and quantity. Both table have add button to add rows if needed. i have 3 tables in database 'add_stock' in which general info is added, 'Product_size' in which size is added and 'product_color' in which product color is added across particular size. When i insert these values in database product_size table values are inserted fine but it adds all the child table values in parent first row and then for second row it again add all colors from the child table from both rows of parent table. i want that across first row table it should add only child table values of first row and for second row it should add only child table values that are in second row. CODE:
<?php
if (isset($_POST['submit']))
{
$brand = $_POST['brand_name'];
$price = $_POST['price'];
$gender = $_POST['dress_gender'];
$category = $_POST['category'];
$material = $_POST['dress_material'];
$description = $_POST['dress_description'];
$con=mysqli_connect("localhost", "root", "");
mysqli_select_db($con,"login");
$qry="INSERT INTO add_stock (brand_name, price, gender_name, category_name, material_name, dress_description, image, ext) VALUES ('$brand', '$price', '$gender', '$category', '$material', '$description', '$f_name', '$f_extension')";
$result=mysqli_query($con,$qry) or die(mysqli_error($con));
$product_id = mysqli_insert_id($con);
if($result)
{
for ($i=0; $i<count($_POST['size']); $i++){
$size = $_POST['size'][$i];
$qry1="INSERT INTO product_size (product_id, product_size) VALUES ('$product_id', '$size')";
$result1=mysqli_query($con,$qry1);
$product_size_id = mysqli_insert_id($con);
for ($j=0; $j<count($_POST['color']); $j++){
$quantity = $_POST['dress_quantity'][$j];
$color = $_POST['color'][$j];
$qry2="INSERT INTO product_color (product_size_id, product_color, product_quantity) VALUES ('$product_size_id', '$color', '$quantity')";
$result2=mysqli_query($con,$qry2);
if($result2)
{
echo '<script>alert("Record Added Successfully!")</script>';
echo '<script>window.location="try.php"</script>';
}
else
{
die("Error While Adding Stock ! Please Try Again.");
}
}
}
}
}
?>