Given you current structure that could only be a good thing to do.
Your date storage format is useless. Can't be processed or sorted. Always store in yyyy-mm-dd format (type DATE)
You shouldn't store age, it needs continual updating and can be easily derived from a correctly formatted date (SELECT timestampdiff(YEAR, curdate(), birthday) as age )
Description like "SuperCar" should appear once in a database and not be used as FKs. Only ids should occur in more that one table.
If one of your people buys a "Hot Air Balloon" it won't appear on the form to add the option as no-one currenly has one. (In my design I merely add another record to "Vehicle_type" table - job done.)
I have rewritten my query and code to use your database structure. I had to use a subquery to get the available vehicle types instead of my table)
// connect to DB here
if ($_SERVER['REQUEST_METHOD']=='POST') {
// echo '<pre>' . print_r($_POST, 1) . '</pre>';
try {
$db->beginTransaction();
$stmt1 = $db->prepare("DELETE FROM vehicle
WHERE formid = ?
");
$stmt1->execute( [ $_POST['formid'] ] );
if (isset($_POST['trans'])) {
$stmt2 = $db->prepare("INSERT INTO vehicle (formid, vehselection)
VALUES (?, ?)
");
foreach ($_POST['trans'] as $veh) {
$stmt2->execute( [ $_POST['formid'], $veh ] );
}
}
$db->commit();
}
catch(PDOException $e) {
$db->rollBack();
throw $e;
}
header("Refresh: 0");
exit;
}
$res = $db->query("SELECT f.formid
, f.name
, f.lastname
, fv.vehselection
, CASE WHEN v.vehselection IS NULL
THEN 0
ELSE 1
END AS checked
FROM form f
CROSS JOIN
(
SELECT DISTINCT vehselection
FROM vehicle
) fv
LEFT JOIN
vehicle v ON f.formid = v.formid
AND v.vehselection = fv.vehselection
ORDER BY lastname, vehselection
");
$data = [];
// store results in a conveniently structured array
foreach ($res as $r) {
if (!isset($data[$r['formid']])) {
$data[$r['formid']] = [ 'name' => "{$r['name']} {$r['lastname']}",
'trans' => []
];
}
$data[$r['formid']]['trans'][$r['vehselection']] = $r['checked'];
}
//echo '<pre>' . print_r($data, 1) . '</pre>'; # view array structure
//exit;
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="generator" content="PhpED 19.5 (Build 19523, 64bit)">
<title>Example</title>
<meta name="author" content="Barand">
<meta name="creation-date" content="04/09/2022">
<style type='text/css'>
table {
border-collapse: collapse;
margin: 20px auto;
width: 60%;
}
td, th {
padding: 8px;
}
</style>
</head>
<body>
<table border='1'>
<tr>
<th>Name</th>
<th>Transport types</th>
<th> </th>
</tr>
<?php
// output the array
foreach ($data as $fid => $udata) {
echo "<tr>
<form method='post'>
<td>{$udata['name']}</td>
<td>";
foreach ($udata['trans'] as $ttype => $check) {
$chk = $check ? 'checked' : '';
echo "<label><input type='checkbox' name='trans[]' value='$ttype' $chk>$ttype</label><br>";
}
echo "</td>
<td>
<input type='hidden' name='formid' value='$fid'>
<input type='submit' value='Update'>
</td>
</form>
</tr>
";
}
?>
</table>
</body>
</html>