php - php foreach循环编辑数据库中的错误记录
问题描述
再会,
我的网站遇到了一些问题。
我有一个表格,可以从我的数据库中提取一个完整的表格。在每条记录旁边,我有一个“编辑”和“删除”按钮(见图 1)。
图 1
如果我单击记录“删除我”旁边的编辑按钮,则会出现一个模式并使用当前可用数据填充每个可编辑字段的值,然后我可以编辑每个值并更新数据库。这工作正常。
我遇到的问题;当我选择“名称”字段与不同的“填充/尺寸”重复的记录之一时(例如,如果我选择“填充/尺寸”5),它总是使用来自的数据更新和预填充模态表中共享相同“名称”的第一条记录。图 2 显示我单击记录 1-5 的“编辑”按钮。
图 2
我尝试在我的 foreach 循环中创建一个 foreach 循环,但这只是多次重复每条记录。我的代码如下。任何意见或建议将不胜感激。
<div class="card mb-3">
<div class="card-header">
<i class="fa fa-table"></i>Items</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" name="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Filling/Size</th>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Edit/Remove</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Filling/Size</th>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Edit/Remove</th>
</tr>
</tfoot>
<tbody>
<?php foreach ($result as $i => $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->filling; ?></td>
<td><?php echo $row->category; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->price; ?></td>
<td id="editRemoveButtonsCell">
<button id="editButton" type="button" class="btn btn-outline-success" data-toggle="modal" data-target="#editItemModal<?php echo $row->name;?>">Edit</button>
<button id="removeButton" type="button" class="btn btn-outline-danger" data-toggle="modal" data-target="#removeItemModal<?php echo $row->name;?>">Remove</button></td>
</tr>
<!-- Delete Modal -->
<div class="modal" id="removeItemModal<?php echo $row->name;?>">
<div class="modal-dialog">
<form>
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Item <?php echo $row->name?></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Are you sure you want to delete item <?php echo $row->name?> <?php echo $row->filling;?>? <b>This cannot be undone!</b>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<input type="hidden" value="<?php echo $row->name;?>" name="deleteItemName" id="deleteItemName">
<input type="hidden" value="<?php echo $row->filling;?>" name="deleteItemFilling" id="deleteItemFilling">
<button type="button" class="btn btn-cancel" data-dismiss="modal">Close</button>
<input type="submit" name="deleteItem" id="deleteItem" value="Delete" class="btn btn-danger" />
</div>
</div>
</form>
</div>
</div>
<!-- Edit Modal -->
<div class="modal" id="editItemModal<?php echo $row->name;?>" name="editItemModal<?php echo $row->name;?>">
<div class="modal-dialog">
<form>
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Edit Item <?php echo $row->name;?></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<label for="itemNameUpdate">Name</label>
<input type="text" class="form-control" id="itemNameUpdate" name="itemNameUpdate" placeholder="Enter Item Name" value="<?php echo $row->name;?>">
<input type="hidden" value="<?php echo $row->name;?>" name="itemNameOriginal" id="itemNameOriginal">
<input type="hidden" value="<?php echo $row->filling;?>" name="itemFillingOriginal" id="itemFillingOriginal">
</div>
<div class="form-group">
<label for="itemFillingUpdate">Filling/Size</label>
<input type="text" class="form-control" id="itemFillingUpdate" name="itemFillingUpdate" placeholder="Enter Filling/Size" value="<?php echo $row->filling;?>">
</div>
<div class="form-group">
<label for="itemCategoryUpdate">Category</label>
<select class="form-control" id="itemCategoryUpdate" name="itemCategoryUpdate">
<?php echo '<option>'.$row->category.'</option>';?>
<?php foreach($categories as $category) {echo '<option>'.$category->name.'</option>';}?>
</select>
</div>
<div class="form-group">
<label for="itemDescriptionUpdate">Description</label>
<textarea class="form-control" id="itemDescriptionUpdate" name="itemDescriptionUpdate" rows="3" placeholder="Item description (optional)"><?php echo $row->description;?></textarea>
</div>
<div class="form-group">
<label for="itemPriceUpdate">Price</label>
<input type="text" class="form-control" id="itemPriceUpdate" name="itemPriceUpdate" placeholder="Enter Price" value="<?php echo $row->price;?>">
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-cancel" data-dismiss="modal">Close</button>
<input type="submit" name="editItem" id="editItem" value="Edit" class="btn btn-success" />
</div>
</div>
</form>
</div>
</div>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="card-footer small text-muted">Use the navigation bar to the left to edit the menu.</div>
</div>
</div>
解决方案
您所有的代码引用都指向$row->name
,我认为按名称引用代码中的任何内容都是不好的做法。您希望通过每个案例的唯一标识符(通常是数字)进行识别。所以你不会有这种模棱两可的感觉。
图 2 显示我单击记录 1-5 的“编辑”按钮。
您无法在此问题中清楚地识别您自己的行,而无需提取只是巧合不同的外部数据。
您想要的是数据库表的每一行的唯一标识符。
这是一个绝对的基本标准,可以在MySQL Dev Tutorial Here中阅读。
这个 SO question也可能对您有帮助。
所以你可以更新你的 MySQL(假设那是你的数据库):
ALTER TABLE users ADD uid SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (uid);
(另请参阅此处了解哪种 int 类型最适合您的情况)
为每一行设置一个唯一标识符后,您需要在代码中使用该标识符,例如:
<input type="hidden" value="<?php echo $row->uid;?>"
name="deleteItemUniqueId" id="deleteItemUniqueId">
和
<div class="modal" id="editItemModal<?php echo $row->uid;?>"
name="editItemModal<?php echo $row->uid;?>">
并且同样将其应用于您的代码库。
这停止了 HTML 的任何重复,id
并停止了代码逻辑在给定的识别数据部分存在任何歧义的问题。
推荐阅读
- docker - 如何在 Hyperledger Fabric 中找到链码位置?
- postgresql - 在 Ubuntu 中运行 postgres 的最佳实践?
- css - 在 SCSS 中从组中排除元素
- r - 对面板数据进行逻辑回归
- php - 使用 HTML 的 PHP 中的单元格超链接
- oracle - 如何使用包含表名的已声明变量作为 for 循环中的连接。甲骨文 PL/SQL
- angular - 在其他模块中使用模块时,覆盖内容
- web - 当公钥在 PEM 中分发时,如何使用 Web Crypto 验证 ES256 JWT 令牌?
- c# - 如何将 JSON 反序列化为 IList
> 键中有空格? - audiokit - 如何获得对触摸事件的最低音频延迟响应?