html - how to associate child table values with parent table row and inserts child tables values corresponds to parent table row in php -
i have 2 nested tables. parent table row contains child table. both tables can add rows if needed add button. parent table values insert in product_size table in database , child table values inserts in product_color table. parent table contains sizes , child table contains color , quantity of items of size. want insert child table values(color & quantity) across parent table row. means parent first row child table values should insert across parent first row , child table values in second row of parent table should insert across parent second row. currently, code takes child table values rows of parent table , inserts in database across first row , takes child table values rows of parent table again , inserts in database across second row of parent table. please check code , me point out problem in code. /php code/
if (isset($_post['submit'])) { $con=mysqli_connect("localhost", "root", ""); mysqli_select_db($con,"login"); ($i=0; $i<count($_post['size']); $i++){ $size = $_post['size'][$i]; $qry1="insert product_size (product_size) values ('$size')"; $result1=mysqli_query($con,$qry1); $product_size_id = mysqli_insert_id($con); ($j=0; $j<count($_post['color']); $j++){ $quantity = $_post['dress_quantity'][$j]; $color = $_post['color'][$j]; $qry2="insert 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."); } } } }
i tested on local server.work's fine
$con=mysqli_connect("localhost", "root", ""); mysqli_select_db($con,"login"); // $size1 = array("50", "60", "70"); // $color1 =array("red", "blue", "green"); // $quantity1 = array("10", "20", "30"); ($i=0; $i<count($_post['size']); $i++){ $size = $size1[$i]; $qry1="insert product_size (product_size) values ('$size')"; $result1=mysqli_query($con,$qry1); var_dump($result1); echo "<br>"; $product_size_id = mysqli_insert_id($con); ($j=0; $j<count($size); $j++){ $quantity = $_post['dress_quantity'][$i]; $color = $_post['color'][$i]; $qry2="insert 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."); } } }
Comments
Post a Comment