Mysql
 sql >> Base de données >  >> RDS >> Mysql

comment faire correspondre deux lignes de la même table mysql en php

Tout d'abord, vous devez créer un tableau qui contiendra toutes les tailles souhaitées. Le tableau des tailles ne doit avoir qu'un identifiant pour la taille et un nom pour la taille, puis utilisez ce tableau pour créer votre liste déroulante de taille.

Ensuite, faites quelque chose comme ça sur votre page de tableau HTML.

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

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

puis dans votre 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')" />

Maintenant, lorsque vous recevez les données sur votre PHP de traitement, faites quelque chose comme ça.

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.");
       }
   }
}

Cela peut contenir des erreurs c'est l'idée de base et je ne suis pas très à l'aise avec mysqli_* donc s'il y en a, faites le moi savoir pour les corriger.