我目前在表格的每一行旁边都有一个删除和更新按钮。删除按钮的工作原理是从表中删除该行,但更新按钮仅适用于表中的最后一行。如果我尝试在此之前更改一行中的字段并按更新 - 它将更新为最后一行的值......我该如何解决这个问题?(以便可以单独更新每一行)。
while($row = mysql_fetch_assoc($result))
{
echo
"
<tr>
<td style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['topic_id'] . "</td>
<td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_name' value='" . $row['topic_name'] . "'></td>
<td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_address' value='" . $row['topic_address'] . "'></td>
<td><button type='submit' name='updateTopic' value='".$row['topic_id']."'>Update</button></td>
<td><button type='submit' name='deleteTopic' value='".$row['topic_id']."'>Delete</button></td>
</tr>
";
}
if(isset($_POST['updateTopic'])){
$updateID = $_POST['updateTopic'];
$updateTopicName = $_POST['up_topic_name'];
$updateTopicAddress = $_POST['up_topic_address'];
$updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
if(isset($_POST['deleteTopic'])){
$deleteID = $_POST['deleteTopic'];
$deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
$deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
echo "</form></table>";
两个按钮都是形式上的,因此它们将始终存在于POST
数组中
通过请求执行编辑和删除的更快方法是$_GET
并将您的链接设为
http://localhost/project_name/file_name?id=".$row['topic_id']."&operation=delete
对于编辑:
http://localhost/project_name/file_name?id=".$row['topic_id']."&operation=edit
在服务器端,添加此检查
if(isset($_GET['operation']) && trim($_GET['operation'])=="edit")){
// Edit code
}
else if(isset($_GET['operation']) && trim($_GET['operation'])=="delete")){
// Delete code
}
问题是因为所有"名称"和"地址"字段都具有相同的输入名称...
当您尝试读取 $updateTopicName = $_POST['up_topic_name'] 时,您想要什么up_topic_name? 第一排的那个? 第二排的那个? 提交时,名为 up_topic_name 的所有输入将被最后一个值覆盖。
要解决此问题,您必须(仅)命名输入up_topic_name并up_topic_address如下所示:
<input type='text' name='up_topic_name[{$row['topic_id']}]' value='" . $row['topic_name'] . "'>
然后在读取值时,执行以下操作:
$updateID = $_POST['updateTopic'];
$updateTopicName = $_POST['up_topic_name'][$updateID];
HTML 结构不正确(tr>形式> td ^^很糟糕),但这样应该更好。为了获得最佳结构,您应该使用div 而不是 table。这个想法是 1 个动作 = 1 个形式:
<?php
if(isset($_POST['updateTopic'])){
$updateID = $_POST['updateTopic'];
$updateTopicName = $_POST['up_topic_name'];
$updateTopicAddress = $_POST['up_topic_address'];
$updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
if(isset($_POST['deleteTopic'])){
$deleteID = $_POST['deleteTopic'];
$deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
$deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>
<form method='POST' action=''>
<input type='hidden' name='updateTopic' value='".$row['topic_id']."'>
<td style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['topic_id'] . "</td>
<td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_name' value='" . $row['topic_name'] . "'></td>
<td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_address' value='" . $row['topic_address'] . "'></td>
<td><button type='submit'>Update</button></td>
</form>
<form method='POST' action=''>
<input type='hidden' name='deleteTopic' value=".$row['topic_id'].">
<td><button type='submit'>Delete</button></td>
</form>
</tr>";
}
echo "</table>";
注意:我从获取行中移出了您的更新/删除操作,因为您只需要执行一次。例如,使用代码,您可以在每个循环中删除。