使用php和jquery从2个表中检索多个数据

使用php和jquery从2个表中检索多个数据,php,jquery,Php,Jquery,我从印尼程序员那里得到了这段代码,这段代码只从一个表中检索多个数据 我的问题是如何从两个表中检索多个数据并将它们保存到另一个表中 结果就是我想要达到的效果 这是桌子 **request** prNo branch pr03 odessa pr04 kiev **detail_request** prNo productCode productName qty pr03 111 soap 1200 pr03 112 tooth

我从印尼程序员那里得到了这段代码,这段代码只从一个表中检索多个数据

我的问题是如何从两个表中检索多个数据并将它们保存到另一个表中

结果就是我想要达到的效果

这是桌子

**request**
prNo   branch
pr03   odessa
pr04   kiev


**detail_request**
prNo productCode productName   qty
pr03   111        soap         1200
pr03   112        tooth paste  1000
我想将详细信息请求表上的数据保存到购买表中,但只保存复选框上有复选标记的数据,并且我正在添加要手动填充的价格列

这是密码

<html>
    <head>
        <title>Lookup Modal Bootstrap 3</title>
        <link rel="stylesheet" href="bootstrap/css/bootstrap.css"/>
        <link rel="stylesheet" href="datatables/dataTables.bootstrap.css"/>
        <style>
            body{
                margin: 15px;
            }
            .pick:hover{
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div class="row">
            <div class="col-md-5">
                <h2>&nbsp;</h2>
            </div>
        </div>
        <form action="action" onsubmit="dummy();
                return false">
            <div class="form-group">
                <label for="varchar">Request Number</label>
                <div class="row">
                    <div class="col-md-4">
                        <input type="text" class="form-control" name="prNo" id="prNo" placeholder="Request Number" readonly />
                          <strong>Branch Name</strong><br>
                          <input type="text" class="form-control" name="branch" id="branch" placeholder="branch " readonly />

                    </div>

                    <div class="col-md-2">

                      <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">. . .</button>
                    </div>
                </div>
            </div>
<table width="446" border="1">
  <tr>
    <th scope="row">&nbsp;</th>
    <th scope="row">Request Number</th>
    <td><strong>Product Code</strong></td>
    <td><strong>Product Name</strong></td>
    <td><strong>QTY</strong></td>
    <td><strong>Price</strong></td>
  </tr>
  <tr>
    <th scope="row"><input type="checkbox" name="prNo" id="prNo"></th>
    <th scope="row"><label for="Request Number"></label>
      <input type="text" name="prNo" id="prNo"></th>
    <td><label for="productCode"></label>
      <input type="text" name="productCode" id="productCode"></td>
    <td><label for="productName"></label>
      <input type="text" name="productName" id="productName"></td>
    <td><label for="qty"></label>
      <input type="text" name="qty" id="qty"></td>
    <td><input type="text" name="price" id="price"></td>
  </tr>
  <tr>
    <th scope="row"><input type="checkbox" name="prNo4" id="prNo4"></th>
    <th scope="row"><input type="text" name="prNo2" id="prNo2"></th>
    <td><input type="text" name="productCode2" id="productCode2"></td>
    <td><input type="text" name="productName2" id="productName2"></td>
    <td><input type="text" name="qty2" id="qty2"></td>
    <td><input type="text" name="price2" id="price2"></td>
  </tr>
</table>


              <input type="submit" value="Save" class="btn btn-primary" />


        </form>

        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog" style="width:800px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Lookup </h4>
                    </div>
                    <div class="modal-body">
                        <table id="lookup" class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Request Number</th>
                                    <th>Branch Name</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php

                                $con = mysqli_connect('localhost', 'root', '', 'purchase');
                                $sql = mysqli_query($con,'select * from request ');
                                while ($r = mysqli_fetch_array($sql)) {
                                    ?>
                                    <tr class="pick" no="<?php echo $r['prNo']; ?>", branch="<?php echo $r['branch'] ?>", code="<?php echo $r['productCode'] ?>">
                                        <td><?php echo $r['prNo']; ?></td>
                                        <td><?php echo $r['branch']; ?></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>  
                    </div>
                </div>
            </div>
        </div>
        <script src="js/jquery-1.11.2.min.js"></script>
        <script src="bootstrap/js/bootstrap.js"></script>
        <script src="datatables/jquery.dataTables.js"></script>
        <script src="datatables/dataTables.bootstrap.js"></script>
        <script type="text/javascript">


            $(document).on('click', '.pick', function (e) {
                document.getElementById("prNo").value = $(this).attr('no');
                document.getElementById("branch").value = $(this).attr('branch');

                $('#myModal').modal('hide');
            });



            $(function () {
                $("#lookup").dataTable();
            });


        </script>
    </body>
</html>

查找模式引导3
身体{
利润率:15px;
}
.选择:悬停{
光标:指针;
}
请求号码
分行名称
. . . 请求号码 产品代码 产品名称 数量 价格 &时代; 查找 请求号码 分支机构名称 $(文档).on('click','.pick',函数(e){ document.getElementById(“prNo”).value=$(this.attr('no'); document.getElementById(“分支”).value=$(this.attr(“分支”); $('#myModal').modal('hide'); }); $(函数(){ $(“#查找”).dataTable(); });
需要你的帮助吗

感谢您提供的高级


<?php
if(isset($_POST['product_submit']))
{
    $check=$_POST['check'];
    foreach($check as $i)
    {
        $prno=$_POST['prNo'.$i];
        $prcode=$_POST['productCode'.$i];
        $prname=$_POST['productName'.$i];
        $qty=$_POST['qty'.$i];
        $price=$_POST['price'.$i];
        $query = mysqli_query($con,"insert into purchase (prNo,productCode,productName,qty,price) value ('$prno', '$prcode', '$prname', '$qty', '$price')");
    }
    if($query)
    {
    ?>
    <script>
    alert("success");
    </script>
    <?php
    }
}
?>

<html>
    <head>
        <title>Lookup Modal Bootstrap 3</title>
    </head>
    <body>
        <div class="row">
            <div class="col-md-5">
                <h2>&nbsp;</h2>
            </div>
        </div>
        <form method="post" id="my_form">
            <div class="form-group">
                <label for="varchar">Request Number</label>
                <div class="row">
                    <div class="col-md-4">
                        <input type="text" class="form-control" name="prNo" id="prNo" placeholder="Request Number" readonly />
                          <strong>Branch Name</strong><br>
                          <input type="text" class="form-control" name="branch" id="branch" placeholder="branch " readonly />

                    </div>

                    <div class="col-md-2">

                      <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">. . .</button>
                      <button type="button" class="btn btn-default" onclick="show_fun()">View</button>
                    </div>
                </div>
            </div>

<table width="446" border="1">
  <thead>
  <tr>
    <th scope="row">&nbsp;</th>
    <th scope="row">Request Number</th>
    <td><strong>Product Code</strong></td>
    <td><strong>Product Name</strong></td>
    <td><strong>QTY</strong></td>
    <td><strong>Price</strong></td>
    <td><strong>Total</strong></td>
  </tr>
  </thead>
  <tbody id="product_table">

  </tbody>
</table>


              <input type="submit" value="Save" name="product_submit" class="btn btn-primary" />


        </form>

        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog" style="width:800px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Lookup </h4>
                    </div>
                    <div class="modal-body">
                        <table id="lookup" class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Request Number</th>
                                    <th>Branch Name</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php

                                $con = mysqli_connect('localhost', 'root', '', 'purchase');
                                $sql = mysqli_query($con,'select * from request ');
                                while ($r = mysqli_fetch_array($sql)) {
                                    ?>
                                    <tr class="pick" no="<?php echo $r['prNo']; ?>", branch="<?php echo $r['branch'] ?>", code="<?php echo $r['productCode'] ?>">
                                        <td><?php echo $r['prNo']; ?></td>
                                        <td><?php echo $r['branch']; ?></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>  
                    </div>
                </div>
            </div>
        </div>
        <script src="js/jquery-1.11.2.min.js"></script>
        <script src="bootstrap/js/bootstrap.js"></script>
        <script src="datatables/jquery.dataTables.js"></script>
        <script src="datatables/dataTables.bootstrap.js"></script>
        <script type="text/javascript">


            $(document).on('click', '.pick', function (e) {
                document.getElementById("prNo").value = $(this).attr('no');
                document.getElementById("branch").value = $(this).attr('branch');

                var no=$(this).attr('no');

                $.ajax({
                    type:"post",
                    data:"&product_id=1&prno="+no,
                    url:"ajax.php",
                    success:function(result)
                    {
                        $("#product_table").html(result);
                    }
                });
                $('#myModal').modal('hide');
            });

            $(function () {
                $("#lookup").dataTable();
            });

            function calc_fun(i){
             var qty=$("#qty"+i).val();
             var price=$("#price"+i).val();
             var total=Number(qty)*Number(price);
             $("#total"+i).val(total);
            }

        </script>
    </body>
</html>

为什么使用modal和form?我使用modal加载数据表,这样用户可以从数据表中选择一个请求并将数据发送到主页确定,为什么将readonly属性设置为input BOX只读以防止用户更改数据,用户使用。。。按钮加载数据并选择一个。请像我在问题中描述的那样创建表,并将我的代码复制粘贴到编辑器中,它将顺利运行,您将了解我想要实现的目标。对于reply@praveena,我将对其进行测试并通知您结果。我为什么会收到通知:未定义变量:con in D:\xampp\htdocs\modalukap\ajax.php,在第6行,警告:mysqli_query()期望参数1为mysqli,在第6行的D:\xampp\htdocs\modalukap\ajax.php中为null,警告:mysqli_fetch_数组()期望参数1为mysqli_结果,在第7行的D:\xampp\htdocs\modalukap\ajax.php中为null
$con=mysqli_connect('localhost','root','purchase')将此添加到ajax页面的顶部supppeerrrrbbbb,非常感谢,您的代码工作得非常完美,上帝保佑您@pravenethank you@Shevcenko,一切顺利
<?php
if(isset($_POST['product_submit']))
{
    $check=$_POST['check'];
    foreach($check as $i)
    {
        $prno=$_POST['prNo'.$i];
        $prcode=$_POST['productCode'.$i];
        $prname=$_POST['productName'.$i];
        $qty=$_POST['qty'.$i];
        $price=$_POST['price'.$i];
        $query = mysqli_query($con,"insert into purchase (prNo,productCode,productName,qty,price) value ('$prno', '$prcode', '$prname', '$qty', '$price')");
    }
    if($query)
    {
    ?>
    <script>
    alert("success");
    </script>
    <?php
    }
}
?>

<html>
    <head>
        <title>Lookup Modal Bootstrap 3</title>
    </head>
    <body>
        <div class="row">
            <div class="col-md-5">
                <h2>&nbsp;</h2>
            </div>
        </div>
        <form method="post" id="my_form">
            <div class="form-group">
                <label for="varchar">Request Number</label>
                <div class="row">
                    <div class="col-md-4">
                        <input type="text" class="form-control" name="prNo" id="prNo" placeholder="Request Number" readonly />
                          <strong>Branch Name</strong><br>
                          <input type="text" class="form-control" name="branch" id="branch" placeholder="branch " readonly />

                    </div>

                    <div class="col-md-2">

                      <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">. . .</button>
                      <button type="button" class="btn btn-default" onclick="show_fun()">View</button>
                    </div>
                </div>
            </div>

<table width="446" border="1">
  <thead>
  <tr>
    <th scope="row">&nbsp;</th>
    <th scope="row">Request Number</th>
    <td><strong>Product Code</strong></td>
    <td><strong>Product Name</strong></td>
    <td><strong>QTY</strong></td>
    <td><strong>Price</strong></td>
    <td><strong>Total</strong></td>
  </tr>
  </thead>
  <tbody id="product_table">

  </tbody>
</table>


              <input type="submit" value="Save" name="product_submit" class="btn btn-primary" />


        </form>

        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog" style="width:800px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Lookup </h4>
                    </div>
                    <div class="modal-body">
                        <table id="lookup" class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Request Number</th>
                                    <th>Branch Name</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php

                                $con = mysqli_connect('localhost', 'root', '', 'purchase');
                                $sql = mysqli_query($con,'select * from request ');
                                while ($r = mysqli_fetch_array($sql)) {
                                    ?>
                                    <tr class="pick" no="<?php echo $r['prNo']; ?>", branch="<?php echo $r['branch'] ?>", code="<?php echo $r['productCode'] ?>">
                                        <td><?php echo $r['prNo']; ?></td>
                                        <td><?php echo $r['branch']; ?></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>  
                    </div>
                </div>
            </div>
        </div>
        <script src="js/jquery-1.11.2.min.js"></script>
        <script src="bootstrap/js/bootstrap.js"></script>
        <script src="datatables/jquery.dataTables.js"></script>
        <script src="datatables/dataTables.bootstrap.js"></script>
        <script type="text/javascript">


            $(document).on('click', '.pick', function (e) {
                document.getElementById("prNo").value = $(this).attr('no');
                document.getElementById("branch").value = $(this).attr('branch');

                var no=$(this).attr('no');

                $.ajax({
                    type:"post",
                    data:"&product_id=1&prno="+no,
                    url:"ajax.php",
                    success:function(result)
                    {
                        $("#product_table").html(result);
                    }
                });
                $('#myModal').modal('hide');
            });

            $(function () {
                $("#lookup").dataTable();
            });

            function calc_fun(i){
             var qty=$("#qty"+i).val();
             var price=$("#price"+i).val();
             var total=Number(qty)*Number(price);
             $("#total"+i).val(total);
            }

        </script>
    </body>
</html>
<?php
if(isset($_POST['product_id']))
{
    $prno=$_POST['prno'];
    $i=1;
    $sql = mysqli_query($con,"select * from detail_request where prNo='$prno'");
    while ($r = mysqli_fetch_array($sql)) {
        echo '<tr>
    <th scope="row"><input type="checkbox" name="check[]" id="check'.$i.'" value="'.$i.'"></th>
    <th scope="row"><label for="Request Number"></label>
      <input type="text" name="prNo'.$i.'" id="prNo'.$i.'" readonly value="'.$r["prNo"].'"></th>
    <td><label for="productCode"></label>
      <input type="text" name="productCode'.$i.'" id="productCode'.$i.'" readonly value="'.$r["productCode"].'"></td>
    <td><label for="productName"></label>
      <input type="text" name="productName'.$i.'" id="productName'.$i.'" readonly value="'.$r["productName"].'"></td>
    <td><label for="qty"></label>
      <input type="text" name="qty'.$i.'" id="qty'.$i.'" onchange="calc_fun('.$i.')" readonly value="'.$r["qty"].'"></td>
    <td><input type="text" name="price'.$i.'" onchange="calc_fun('.$i.')" id="price'.$i.'"></td>
    <td><input type="text" name="total'.$i.'" id="total'.$i.'"></td>
  </tr>';
  $i++;
    }
}
?>