I have Html table in which row can be added by add button if needed. Row contains size dropdown and color dropdown. In database i have 2 tables product_size and product_color, i have a condition that if two row sizes values are same then it should have same product_size_id(that is primary key of product_size table otherwise different product_size_id). I am sharing my code please help if anyone can point out where problem is. CODE:
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);
$insertid=mysqli_insert_id($con);
if($result1)
{
$newqry="SELECT * from product_size where product_size_id='".$insertid."'";
$newresource=mysqli_query($con,$newqry);
$newresult=mysql_fetch_array($newresource);
if ($newresult['product_size']== $_POST['size'][$i])
{
$product_size_id= $newresult['product_size_id'];
}
else
{
$product_size_id = $insertid; /*it always going to this condition*/
}
$quantity = $_POST['dress_quantity'][$i];
$color = $_POST['color'][$i];
$qry2="INSERT INTO product_color (product_size_id, product_color, product_quantity) VALUES ('$product_size_id', '$color', '$quantity')";
$result2=mysqli_query($con,$qry2);
echo '<script>alert("Item Added Successfully!")</script>';
echo '<script>window.location="try.php"</script>';
}
}
//HTML CODE:
<TABLE id="dataTable">
<thead>
<tr>
<th style="text-align: center;"> Select </th>
<th style="text-align: center;"> <b>Size</b> </th>
<th style="text-align: center;"> <b>Color</b> </th>
<th><b>Quantity</b></th>
</tr>
</thead>
<tbody>
<tr id='C1' class='customer'>
<td><input type="checkbox" name="chk"/></td>
<td><select name="size[]" id="size" required="" >
<option value="">Select Size</option>
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
<option value="X-Large">X-Large</option>
</select></td>
<td>
<select name="color[]" required="" >
<option value="">Select Color</option>
<option value="Aqua">Aqua</option>
<option value="Blue">Blue</option>
<option value="Black">Black</option>
<option value="Green">Green</option>
</select></td>
<td>
<input style="width: 120px; height: 26px; " type="number" name="dress_quantity[]" class="qty1" onchange="calculate();" min="1" max="1000" maxlength="4" placeholder="Size Quantity" value="" required="">
</td>
</tr>
</tbody>
</TABLE>
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />