如何在php中匹配两行mysql相同的表 [英] how to match two rows of mysql same table in php

查看:75
本文介绍了如何在php中匹配两行mysql相同的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,可以通过添加按钮添加行。我在我的数据库product_size和product_color中有两个表。 Product_color表具有包含大小ID的列。我试图做到这一点,如果两个或两个以上的行的大小表是相同的,例如小,那么它应该插入相同的size_id到颜色表的所有小颜色。请检查我的代码和帮助,如果有人可以。


product_color表



CODE:

  if(isset($ _ POST ['submit']) )
{
$ con = mysqli_connect(localhost,root,);
mysqli_select_db($ con,login); $
$ b $($ i = 0; $ i< count($ _ POST ['size']); $ i ++){

$ size = $ _POST ['size' ] [$ i]于;

$ qry1 =INSERT INTO product_size(product_size)VALUES('$ size');

$ result1 = mysqli_query($ con,$ qry1);
$ product_size_id = mysqli_insert_id($ con);


$数量= $ _POST ['dress_quantity'] [$ i];

$ color = $ _POST ['color'] [$ i];
$ b $ qry2 =插入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(添加库存时出错!请再试一次。);
}
}
}

// HTML代码:

 < TABLE id =dataTable> 
< thead>
< tr>
< th style =text-align:center;>& nbsp;选取& nbsp;< / th>
< th style =text-align:center;>& nbsp;< b>大小< / b& nbsp;< / th>
< th style =text-align:center;>& nbsp;< b> Color< / b>& nbsp;< / th>
< th>< b>数量< / b>< / th>
< / tr>
< / thead>

< tbody>
< tr id ='C1'class ='customer'>
< td>< input type =checkboxname =chk/>< / td>
< td>< select name =size []id =sizerequired =>
< option value =>选择大小< / option>< / select>< / td>
< td>
< option value =>选择颜色< / option>
< option value =Aqua> Aqua< / option>
< option value =Blue>蓝色< / option>
< option value =Black>黑色< / option>
< option value =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 。最长长度); type =numbername =dress_quantity []class =qty1onchange =calculate(); min =1max =1000maxlength =4placeholder =Size Quantityvalue =required =>
< / td>
< / tr>
< / tbody>
< / TABLE>
< INPUT type =buttonvalue =添加行onclick =addRow('dataTable')/>
< INPUT type =buttonvalue =Delete Rowonclick =deleteRow('dataTable')/>

现在当您接收到处理数据时,PHP会做这样的事情。

  if(isset($ _ POST ['submit'])){

$ con = mysqli_connect(localhost, 根, );
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 - >准备($ qry2);
$ result2 - > bind_param(isi,$ product_size_id,$ color,$ quantity);
$ b $ if($ result2 - > execute()){
echo'< script> alert(Record Added Successfully!)< / script>';
echo'< script> window.location =try.php< / script>';
} else {
die(添加库存时发生错误!请再试一次。);





$ b

这可能包含错误基本的想法和我不太熟悉 mysqli _ * ,所以如果有任何让我知道的改正它们的话。


I have a Table in which rows can be added if needed by add button. i have two tables in my database product_size and product_color. Product_color table has column that contains size id. I am trying to do that if two or more rows value are same in size table e.g "small" then it should insert same size_id to the color table for all small colors. Please check my code and help if anyone can.

product_color table

CODE:

 if (isset($_POST['submit']))
{   
    $con=mysqli_connect("localhost", "root", "");
    mysqli_select_db($con,"login"); 

    for ($i=0; $i<count($_POST['size']); $i++){

        $size = $_POST['size'][$i];          

        $qry1="INSERT INTO product_size (product_size) VALUES ('$size')";

        $result1=mysqli_query($con,$qry1);
        $product_size_id = mysqli_insert_id($con);                    


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

//HTML CODE:

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

//HTML TABLE

解决方案

First of you need to create a table which will have all the sizes you want. The size table should have only id for the size and a name for the size then use that table to create your size drop down.

Then do something like this on your HTML table page.

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

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

then in your 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')" />

Now when you receive the data on to your processing PHP do something like this.

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

This might contain errors this is the basic idea and I'm not much fluent with mysqli_* so if there's any let me know to correct them.

这篇关于如何在php中匹配两行mysql相同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆