I have created a form to update/edit rows in my database. This form works on updating the ID related to selected row that is updated. The page that this is selected from is displayed based on a different parameter/column than the ID. When I submit the form, I am trying to return to this page, but I cannot seem to get it to work based on this parameter, as it seems to return null instead of the update row. I have attached my coding below. edit.php
$id = (int) ($_GET['id'] ?? 0);
$birdRepository = new BirdRepository($pdo);
$model = $birdRepository->fetchById($id);
//var_dump($model);
if (empty($model)) {
header('Location: index.php');
die();
}
if (!empty($_POST)) {
$type = (string) ($_POST['type']);
$dob = (string)($_POST['dob']);
$tagColour = (string) ($_POST['tagColour'] ?? '');
$qty = (int) ($_POST['qty']);
if (
empty($type) ||
empty($dob) ||
empty($tagColour) ||
empty($qty)
) {
header('Location: index.php');
die();
}
$model = $birdRepository->update(
$id,
$type,
$dob,
$tagColour,
$qty
);
header('location: bird.php');
// return;
}
render('edit.view', [
'birds' => $model
]);
update function:
public function update(int $id, string $type, string $dob, string $tagcolour, int $qty)
{
$stmt = $this->pdo->prepare(
'UPDATE `birds`
SET
`type` = :type,
`dob` = :dob,
`tag_colour` = :tagColour,
`qty` = :qty
WHERE `id` = :id'
);
$stmt->bindValue(':id', $id);
$stmt->bindValue(':type', $type);
$stmt->bindValue(':dob', $dob);
$stmt->bindValue(':tagColour', $tagcolour);
$stmt->bindValue(':qty', $qty);
$stmt->execute();
return $this->fetchById($id);
}
}
edit.view
Update existing entry
bird.view (this is the page I want it to return too
Bird:
DOB
Current Age
Leg Tag Colour
Qty
dob))); ?>
dob); ?>
tagColour); ?>
qty); ?>
Edit
This the error I get on that page after a form is submitted:
Warning: foreach() argument must be of type array|object, null given in C:\xampp\htdocs\bird ages\views\pages\bird.view.php on line 10