0

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.");
                              }
                            }
                          }
                    }
                    }
                    ?>
Shalukh
  • 11
  • 6
  • Not related, but why do you make some tags caps and some not? Surely that takes more effort. – Script47 Sep 06 '17 at 14:10
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Sep 06 '17 at 14:11
  • Is all that html - dozens op options - really relevant to the question? – jeroen Sep 06 '17 at 14:35
  • look at the php code – Shalukh Sep 06 '17 at 14:36
  • Just post the php code if that is what we should look at... – jeroen Sep 06 '17 at 14:36
  • now check i have removed html – Shalukh Sep 06 '17 at 14:38
  • @ jeroen now reply – Shalukh Sep 06 '17 at 15:01

0 Answers0