sql >> Base de Datos >  >> RDS >> Mysql

cómo hacer coincidir dos filas de la misma tabla mysql en php

En primer lugar, debe crear una tabla que tenga todos los tamaños que desee. La tabla de tallas debe tener solo una identificación para la talla y un nombre para la talla, luego use esa tabla para crear su menú desplegable de tallas.

Luego haga algo como esto en su página de tabla HTML.

<?php
$con=mysqli_connect("localhost", "root", "");
mysqli_select_db($con,"login");

$query = mysqli_query("SELECT * FROM size");
$sizeParam = $database->query($query);
$sizeParam -> execute();
?>

luego en tu HTML:

<TABLE id="dataTable">
 <thead>
    <tr>
     <th style="text-align: center;">&nbsp;Select&nbsp;</th>    
     <th style="text-align: center;">&nbsp;<b>Size</b>&nbsp;</th>
     <th style="text-align: center;">&nbsp;<b>Color</b>&nbsp;</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></select></td>
    <?php while($getSizeRow = $sizeParam -> fetch_assoc()){ ?>
      <option id="<?php echo $getSizwRow["sizeId"]; ?>"><?php echo $getSizwRow["sizeId"]; ?></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; " oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" 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')" />

Ahora, cuando reciba los datos en su PHP de procesamiento, haga algo como esto.

if (isset($_POST['submit'])){

    $con=mysqli_connect("localhost", "root", "");
    mysqli_select_db($con,"login"); 

        $quantity = $_POST['dress_quantity'];
        $color = $_POST['color'];
        $size = $_POST['size'];

        $qry2="INSERT INTO product_color (product_size_id, product_color, product_quantity) VALUES (?, ?, ?)";
        $result2= $con -> prepare($qry2);
        $result2 -> bind_param(isi, $product_size_id, $color, $quantity);

        if($result2 -> execute()){
         echo '<script>alert("Record Added Successfully!")</script>';
         echo '<script>window.location="try.php"</script>';
        }else{
                die("Error While Adding Stock! Please Try Again.");
       }
   }
}

Esto puede contener errores, esta es la idea básica y no soy muy fluido con mysqli_* así que si hay alguno, házmelo saber para corregirlo.