如果两行具有相同的值,那么size_id应该在php中保持相同

时间:2017-09-19 08:11:35

标签: php html

我有Html表,如果需要,可以通过添加按钮添加行。行包含大小下拉列表和颜色下拉列表。在数据库中我有2个表product_size和product_color,我有一个条件,如果两个行大小值相同,那么它应该具有相同的product_size_id(这是product_size表的主键,否则不同的product_size_id)。我正在分享我的代码请帮助,如果有人可以指出问题所在。 代码:

{reason: "the reason of the error", details: "some details", type:"the type error"}

// HTML 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>';

  }
}

enter image description here

0 个答案:

没有答案