我的问题与 PHP/SQL 有关。
我知道如何使用“LIMIT 5”来限制返回的结果。如何限制允许的输入?那么用户在删除现有任务之一之前无法添加更多任务吗?所以基本上我如何限制输入而不是结果。此时我仍然可以添加任务,但结果有限,它会将旧任务推到视野之外。我希望不可能添加更多任务,除非在达到列表限制时将其删除。
更新:共享代码:
(是的,我知道我需要使用准备好的语句)
头:
<?php
$errors = "";
$db = mysqli_connect("localhost", "root", "", "certanet");
if (isset($_POST['submit'])) {
if (empty($_POST['task'])) {
$errors = "";
}else{
$task = $_POST['task'];
$sql = "INSERT INTO tasks (task) VALUES ('$task')";
mysqli_query($db, $sql);
header('location: dashboard.php');
}
}
if (isset($_GET['del_task'])) {
$id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE id=".$id);
header('location: dashboard.php');
}
?>
任务列表:
<div class="todobox">
<div class="menutitle" id="category">
Persoonlijke takenlijst
</div>
<div class="inhoud">
Hieronder is het mogelijk om je eigen taken bij te houden.
<form method="post" action="dashboard.php" class="input_form">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
<input type="text" name="task" class="task_input">
<button type="submit" name="submit">Toevoegen</button>
</form>
<table>
<tbody id="todo">
<?php
$tasks = mysqli_query($db, "SELECT * FROM tasks ORDER BY id DESC LIMIT 7");
$i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<tr>
<td> <?php echo $i; ?> </td>
<td class="task"> <?php echo $row['task']; ?> </td>
<td class="delete">
<a href="dashboard.php?del_task=<?php echo $row['id'] ?>">x</a>
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</div>
</div>
侃侃尔雅