我想从“ orders”表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
我想要这个:
inventory_log表格
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>
答案 0 :(得分:0)
仅插入一行,因为您仅发布1个product_id和1个数量
您在php中发布的数据现在看起来像:
<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>
如果要通过帖子发送整个列表,则需要在输入名称中使用数组语法。
类似
$_POST = [
product => [
1 => [
'product_id' => 1,
'quantity' => 9
] ,
2 => [
'product_id' => 2,
'quantity' => 19
] ,
.....
]
]
在您的php脚本中,这将导致POST具有数组值
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!
}
}
您可以使用foreach()进行迭代
from scipy.optimize import rosen, differential_evolution
bounds = [(0, 5), (0, 5), (0, 5), (0, 5), (0, 5)]
result = differential_evolution(rosen, bounds, disp=False)
print(result.x, result.fun)
import matplotlib.pyplot as plt
x, f = zip(*result)
plt.plot(x, f)
但是。使用脚本,您可以继续插入已有的所有行。我不知道这是否只是try / example,但我假设您只想插入新行。现在,您如何设置其继续插入已经拥有的所有行(即使不删除旧行)