html 表单内部 php for 循环 - 编辑/删除按钮
html form inside php for loop - edit / delete buttons
我正在尝试在页面上显示用户保存到数据库中的事件并添加编辑 + 删除按钮。我只使用 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]; ?>">
我正在尝试在页面上显示用户保存到数据库中的事件并添加编辑 + 删除按钮。我只使用 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]; ?>">