我有2个嵌套表。父表具有大小字段,子表位于包含颜色和数量的父表的行中。如果需要,两个表都有添加按钮以添加行。我在数据库'add_stock'中有3个表,其中添加了常规信息,添加了大小的'Product_size'和在特定大小中添加产品颜色的'product_color'。当我在数据库中插入这些值时,product_size表中的值插入正常,但它会在父第一行中添加所有子表值,然后在第二行中再次从父表的两行中添加子表中的所有颜色。我希望在第一行表中它应该只添加第一行的子表值,而第二行它应该只添加第二行中的子表值。 代码:
<?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.");
}
}
}
}
}
?>