繁体   English   中英

从一个表中获取数组数据并插入到另一个表中

[英]Fetch array data from one table and insert into another table

我想从“订单”表中获取数组数据 WHERE order_id =2 并插入到“inventory_log”表中。 但我的代码只插入最后一行

订单表

order_id  product_id  quantity 
     2          1            9
     2          3            2
     3          6            3
     3          5            2
     2          7            1

我要这个:

库存日志表

    id  product_id      quantity 
     1          1            9
     2          3            2
     3          7            1

在下面找到我的代码:

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'store');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Insert into inventory_log TABLE
if(isset($_POST['Submit']))
  {
$productID=$_POST['product_id'];
$IssueQty=$_POST['quantity'];


$inventory=mysqli_query($con,"insert into inventory_log(productID, IssueQty) values('$productID', '$IssueQty')"); 

echo '<script>alert("Sucessful")</script>';
}
?>


<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title></title>
  </head>
  <body>
    <form method="post" name="submit">
    <table border="1">
     <thead>
        <tr>
        <th>Product ID</th>
        <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
    <?php     
    // Get order items from orders TABLE
    $result = $con->query("SELECT order_id, product_id, quantity
    FROM orders
    WHERE order_id = 2"); 
    if($result->num_rows > 0){  
    while($item = $result->fetch_assoc()){ 
    $product_id = $item["product_id"];
    $quantity = $item["quantity"];  
    ?>
    <tr>
      <td><input type="text" name="product_id" value="<?php echo $product_id; ?>"></td> 
      <td><input type="text" name="quantity" value="<?php echo $quantity; ?>"></td>
    </tr>     
    <?php } } ?>
    </tbody>
    </table>
<button type="submit" name="Submit" class="btn btn-success btn-sm">Submit</button>
    <form>
  </body>
</html>

它只插入一行,因为您只发布了 1 个 product_id 和 1 个数量

您在 php 中发布的数据现在看起来像:

$_POST = [
  'product_id' => 1,
  'quantity' => 9
]

如果您想通过帖子发送整个列表,您需要在输入名称中使用数组语法。

就像是

<td><input type="text" name="product[<?= htmlentities($product_id)?>][product_id]" value="<?= htmlentities($product_id); ?>"></td>    
<td><input type="text" name="product[<?= htmlentities($product_id)?>][quantity]" value="<?= htmlentities($quantity); ?>"></td>

在你的 php 脚本中,这将导致一个带有数组值的 POST

$_POST = [
    product => [
       1 => [
          'product_id' =>  1,
          'quantity' =>  9
       ]  ,
       2 => [
          'product_id' =>  2,
          'quantity' =>  19
       ]  ,  
       .....
    ]
] 

您可以使用 foreach() 进行迭代

if( isset( $_POST['product'])) {
    foreach( $_POST['product'] as $product) {
        $productID=$product['product_id'];
        $IssueQty=$product['quantity'];


        // your sql insert here!. please use sql escaping on your user input!
    }
}

然而。 使用您的脚本,您可以不断插入您已有的所有行。 我不知道这是否与尝试/示例一样,但我假设您只想插入新行。 现在如何设置您继续插入您已经拥有的所有行(即使不删除旧行)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM