[php内的html表单进行循环-编辑/删除按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[php内的html表单进行循环-编辑/删除按钮相关的知识,希望对你有一定的参考价值。
我正在尝试在页面上显示用户已保存到数据库并添加编辑+删除按钮的事件。我只使用html,css和php。我知道如何显示事件,但无法找出如何使按钮起作用,因为显示数据位于for循环内。我已经尽力了,但是我不知道如何使它起作用。有人可以帮我吗?我已经准备好使用全新的解决方案了:-)表单代码如下所示:
for($i=0;$i<$count;$i++){
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div class="form-row align-items-center">
<div class="col-auto">
<label class="sr-only" for="editTime">Time</label>
<input type="time" name="editTime" class="form-control mb-2" value="<?php echo $times[$i]; ?>">
</div>
<div class="col-auto">
<label class="sr-only" for="editEvent">Event</label>
<textarea name="editEvent" rows="2" cols="35" id="text" class="form-control"><?php echo $events[$i]; ?></textarea>
</div>
<div class="col-auto">
<input type="submit" class="btn btn-success mb-2" name="btnEdit" value="Edit">
<input type="submit" class="btn btn-danger mb-2" name="btnDel" value="Delete">
</div>
</div>
</form>
<br>
<?php
array_push($casy2, $_POST["editTime"]);
}
以及准备好的查询:
// ready for updating
$stmt=$mysqli->prepare("UPDATE events SET time= ?, event= ? WHERE ID = ?");
$stmt->bind_param("ssi", $newTime, $newEvent, $idevent);
$stmt->execute();
$stmt->close();
// ready for deleting
$stmt = $mysqli->prepare("DELETE FROM events WHERE ID = ?");
$stmt->bind_param("i", $ids[$i]);
$stmt->execute();
$stmt->close();
//user ID
$stmt = $mysqli->prepare("SELECT ID FROM users WHERE nick = ?");
$stmt->bind_param("s", $_SESSION["prezdivka"]);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id);
$stmt->fetch();
$stmt->close();
$eventDate=$_GET["date"];
//users events for the specific date
$stmt = $mysqli->prepare("SELECT ID, time, event FROM events WHERE u_id = ? AND date= ? ORDER BY time ASC");
$stmt->bind_param("is", $id, $eventDate);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No event');
while($row = $result->fetch_assoc()) {
$ids[] = $row['ID']; //event ID
$times[] = $row['time'];
$events[] = $row['event'];
}
$stmt->close();
答案
检查是否已设置任何提交按钮。
if (isset($_POST['btnEdit'])) {
$newTime = $_POST['editTime'];
$newEvent = $_POST['editEvent'];
$idevent = $_POST['eventid'];
$stmt=$mysqli->prepare("UPDATE events SET time= ?, event= ? WHERE ID = ?");
$stmt->bind_param("ssi", $newTime, $newEvent, $idevent);
$stmt->execute();
$stmt->close();
} elseif (isset($_POST['btnDel')) {
$idevent = $_POST['eventid'];
$stmt = $mysqli->prepare("DELETE FROM events WHERE ID = ?");
$stmt->bind_param("i", $idevent);
$stmt->execute();
$stmt->close();
}
您还需要添加具有事件ID的隐藏输入:
<input type="hidden" name="eventid" value="<?php echo $ids[$i]; ?>">
以上是关于[php内的html表单进行循环-编辑/删除按钮的主要内容,如果未能解决你的问题,请参考以下文章
Ionic2 Ion-Input onSubmit 单击表单内的顶部按钮