使用PHP在while循环中使用动态输入字段数组更新MYSQL数据库
我不是新来的,但这是我第一次在这个网站上寻求帮助 我有一个非常有趣的问题,我用整个↓ 靠近底部,与while循环内的其他回声分离↓使用PHP在while循环中使用动态输入字段数组更新MYSQL数据库,php,mysql,foreach,while-loop,Php,Mysql,Foreach,While Loop,我不是新来的,但这是我第一次在这个网站上寻求帮助 我有一个非常有趣的问题,我用整个↓ 靠近底部,与while循环内的其他回声分离↓ <form class="" action="" method="post"> <div class="form-item-special"> <input class="colorize-button" type="submit" name="update_all_stock" value="Upda
<form class="" action="" method="post">
<div class="form-item-special">
<input class="colorize-button"
type="submit" name="update_all_stock" value="Update All" />
</div>
<table>
<thead>
<tr>
<th>Location ID</th>
<th>Item Number</th>
<th>Name</th>
<th>Description</th>
<th>Storage</th>
<th>Edit Stock</th>
<!-- <th>Delete</th> -->
<th>In Stock</th>
<!-- <th>Adjust</th> -->
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['l_id'])) {
$selected_items_id = $_GET['l_id'];
$query = "SELECT * FROM items WHERE item_location_id = '$selected_items_id'";
$selected_items_result = mysqli_query($connection, $query);
if(!$selected_items_result) {
echo "No items found " . mysqli_error($connection);
} else {
while($row = mysqli_fetch_assoc($selected_items_result)) {
$item_id = $row['item_id'];
$item_location_id = $row['item_location_id'];
$item_number = $row['item_number'];
$item_name = $row['item_name'];
$item_description = $row['item_description'];
$item_storage = strtoupper($row['item_storage']);
$item_inventory = $row['item_inventory'];
echo "<tr>";
echo "<td>$item_location_id</td>";
echo "<td>$item_number</td>";
echo "<td>$item_name</td>";
echo "<td>$item_description</td>";
echo "<td>$item_storage</td>";
echo "<td><a href='users.php?source=edit_stock&i_id=$item_id'>Edit Stock</a></td>";
echo "<td>$item_inventory</td>";
echo "<td><input style='width: 50px;' type='number' name='adjust[][".$item_id."]' value='0' /></td>";
echo "</tr>";
} // end while statement
} // end else statement
} // end $_GET['l_id']
?>
</tbody>
</table>
<!-- <div class="form-item-special">
<input class="colorize-button"
type="submit" name="update_all_stock" value="Update All" />
</div> -->
</form>
我已经为此工作了两个多星期,到处寻找答案。请帮忙 循环结束时,
$value
中有什么内容?在我看来,它似乎没有指定要更新的项id,因此它使用相同的值更新所有行,这是循环结束时$value
保留的值,因为每次都会覆盖它
如果调整值不是零,为什么不在循环中运行查询?在中添加一些
echo
语句,以查看变量的值有助于调试。注意$selected\u items\u id,这是sql注入漏洞
if(isset($_POST['update_all_stock'])) {
$i = 0;
foreach ($_POST['adjust'] as $key => $value) {
$new_array = $_POST['adjust'];
$value = $new_array[$key -= $i];
$value = implode(",", $value);
$i ++;
}
$update_stock = "UPDATE items SET ";
$update_stock .= "item_inventory = '". $value ."'";
$update_stock_result = mysqli_multi_query($connection, $update_stock);
if(!$update_stock_result) {
echo "FAILED TO UPDATE STOCK ".mysqli_error($connection);
}
}