0
我有一个表中的数据库生成的内容与编辑和删除按钮在行末尾的形式。为什么要删除按钮空白编辑按钮执行
<td>
<form action="?" method="post">
<div class="">
<input type="hidden" name="id" value="<?php htmlspecialchars($item['id']); ?>">
<input type="hidden" name="action" value="edit_daily_shift_report">
<input type="submit" value="Edit" onclick="return confirm('Edit this report?');">
<input type="hidden" name="action" value="delete_daily_shift_report">
<input type="submit" value="Delete" onclick="return confirm('Delete this report?');">
</div>
</form>
</td>
如果我删除删除按钮,编辑代码工作正常。但是,如果存在两个按钮,则编辑按钮将失败,并且该行中的项目将被删除。我很难过。不仅编辑按钮的动作值被忽略,而且执行删除按钮的动作值。任何帮助表示赞赏!
这里是控制器编辑和删除代码:
/*********************** Edit Daily Shift Report ************************/
if (isset($_POST['action']) && $_POST['action'] === 'edit_daily_shift_report')
{
include '../includes/dbconnect-local.php';
try
{
$sql = 'SELECT * FROM daily_shift_report WHERE id = :id';
$s = $db->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting data.' . $e->getMessage();
include 'error.html.php';
exit();
}
// Assign page title value
$pageTitle = 'Edit Daily Shift Report';
// Store single row resut in $item associative array
$item = $s->fetch(PDO::FETCH_ASSOC);
// Display report content in form
include 'edit_daily_shift_report.html.php';
exit();
}
/********************* Delete from Daily Shift Report *******************/
if (isset($_POST['action']) && $_POST['action'] === 'delete_daily_shift_report')
{
include '../includes/dbconnect-local.php';
try
{
$sql = 'DELETE FROM daily_shift_report WHERE id = :id';
$s = $db->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting data.' . $e->getMessage();
include 'error.html.php';
exit();
}
echo '<span style="color:#ff0000;">Daily Shift Report DELETED successfully!</span>';
}
谢谢。
有一件事我忘了提及。当你有相同名称的字段时,该值将被下一个具有相同名称的字段覆盖。一个例外是,如果您在名称中使用[]。 Php认为这是一个数组,并且会为你创建该字段和数组 – Morten
Thanks @Morten!对不起,我迟迟没有回复。感谢您花时间看看这个。我在解决方案之前解决了这个问题,创建了两个表单,并将它们分别放入自己的表单中。它似乎不是最好的解决方案,但它的工作。但是,您的解决方案更好,我已经在这里和其他地方使用了它。使用
没问题,只是很高兴帮助:) @ JimB814 – Morten