How can I update/insert multiple tasks in my DB? I want to insert or update multiple tasks in my todos website. I can't do that with HTML forms and PHP aren't I?
I have this code
// home.view.php main view file
<table>
<thead>
<tr>
<?php foreach($taskName as $task): ?>
<th><?= strtoupper($task->Field) ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($taskList as $task): ?>
<tr>
<form class="" action="update.php" method="post">
<td>
<label for="id"><?= $task->id ?></label>
<input type="checkbox" name="id[]" value="<?= $task->id ?>" />
</td>
<td>
<label for="description"></label>
<input type="text" name="description" value="<?= $task->description ?>" />
</td>
<td>
<label for="completed"></label>
<input type="checkbox" name="completed" <?= $task->completed ? 'checked value="true"' : '' ?> />
</td>
</form>
<form action="delete.php" method="post">
<td><button type="submit" name="delete" value="<?= $task->id ?>">delete</button></td>
</form>
</tr>
<?php endforeach; ?>
</tbody>
</table>
logic is
// update.php
<?php
require_once 'functions.php';
require_once 'config.php';
if(isset($_POST['completed'])) {
$completed = $_POST['completed'] == 'on' ? 1 : 0;
} else $completed = 0;
$id = $_POST['id'];
$description = htmlentities($_POST['description']);
$query = $dbh->prepare('update todos set description = :description, completed = :completed where id = :id');
$query->execute(array(':description' => $description, ':completed' => $completed, ':id' => $id));
header('Location: index.php');
I tried to insert array of checkboxes, but I don't know how to retrieve all of them in the post, because when I submit a form, I can do only one at a time. Thing is I'm just learning, so if you have advice I will be grateful.
**Edited: removed dd() function for checking $_POST value
Please sign in or create an account to participate in this conversation.