Select multiple rows in table and insert in database
Budget: $15 – $25 USD
I need piece of php code which will allow me to select specific rows via checkbox from a table and then insert them into a mysql database.
So far I have the following code but it does not work:
<tr>
<?php
while ($row = $task->fetch(PDO::FETCH_ASSOC)) {?>
<th scope="row"><?= ($task['task_id']); ?></th>
<td><?= ($row['year']); ?></td>
<td><?= ($row['description']); ?>
<input name="description[]" id="description" value="<?= $row['description']?>" type="hidden">
</td>
<td class="align-middle">
<input name="selector_tasks[]" id="selector_tasks" type="checkbox" value="<?php echo $row['task_id']; ?>">
</td>
</tr>
<button type="submit" name='insertrecords' class="btn btn-primary">Save</button>
<?php
if (isset($_POST['insertrecords'])){
$N = count($_POST['selector_tasks']);
for($i=0; $i < $N; $i++)
{
$task_id = $_POST['selector_tasks'][$i];
$description=$_POST['description'][$i];
$conn->query("
INSERT INTO tasks
(task_id, description)
VALUES ( '$task_id[$i]', '$description[$i]')");
}
}
?>
.
So far I have the following code but it does not work:
<tr>
<?php
while ($row = $task->fetch(PDO::FETCH_ASSOC)) {?>
<th scope="row"><?= ($task['task_id']); ?></th>
<td><?= ($row['year']); ?></td>
<td><?= ($row['description']); ?>
<input name="description[]" id="description" value="<?= $row['description']?>" type="hidden">
</td>
<td class="align-middle">
<input name="selector_tasks[]" id="selector_tasks" type="checkbox" value="<?php echo $row['task_id']; ?>">
</td>
</tr>
<button type="submit" name='insertrecords' class="btn btn-primary">Save</button>
<?php
if (isset($_POST['insertrecords'])){
$N = count($_POST['selector_tasks']);
for($i=0; $i < $N; $i++)
{
$task_id = $_POST['selector_tasks'][$i];
$description=$_POST['description'][$i];
$conn->query("
INSERT INTO tasks
(task_id, description)
VALUES ( '$task_id[$i]', '$description[$i]')");
}
}
?>
.