Show sourcecode
The following files exists in this folder. Click to view.
admin.php
fetch_car_things.php
fetch_kompisar.php
fetch_users.php
mysql1.php
mysql2.php
mysql3.php
mysql3_satt_att_sortera.php
mysql3car.php
mysql3garage.php
mysql3owner.php
welcome.php
mysql3garage.php
138 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
<?php
include('../../incl/dbconnection.php');
/**
* @var PDO $dbconn
*/
try {
if (isset($_POST['garage'])) {
$garage = $_POST['garage'];
$sqlCheck = "SELECT * FROM garage WHERE name=?";
// Kolla om garaget redan finns i tabellen
$check_stmt = $dbconn->prepare($sqlCheck);
$check_stmt->execute([$garage]);
$check_result = $check_stmt->fetch(PDO::FETCH_ASSOC);
if (empty($check_result) || $garage != $check_result['name']) {
$sqlInsert = "INSERT INTO garage (name) VALUES (?)";
$insert_stmt = $dbconn->prepare($sqlInsert);
$insert_stmt->execute([$garage]);
echo "Lade till nytt garage: $garage";
}
else {
echo "<p>Garage finns redan, ingenting gjordes.</p>";
}
} else {
echo "Formuläret är tomt!";
}
} catch (PDOException $e) {
echo "Exception occured: " . $e;
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Ultimate Garage Interface</title>
<style>
table {
border: 1px solid black;
}
#tableContainer tr:nth-child(even) {
background-color: lightgray;
}
th {
border-bottom: 1px solid black;
}
</style>
</head>
<body>
<h1>The Ultimate Garage Interface™</h1>
<form action="" method="POST">
<table>
<h2>Lägg till garage</h2>
<tbody>
<tr>
<td><label for="garage">Namn*:</label></td>
<td><input type="text" name="garage" required></td>
</tr>
<tr>
<td>
<input type="submit" value="Lägg till">
</td>
</tr>
</tbody>
</table>
</form>
<ul>
<li>
<a href="./mysql3car.php">
<button>Lägg till bil -></button>
</a>
</li>
<li>
<a href="./mysql3owner.php">
<button>Lägg till person -></button>
</a>
</li>
</ul>
<button id="showButton">Visa tabeller</button>
<div id="tableContainer"></div>
<script>
const showButton = document.getElementById("showButton");
// Show items in table by clicking button
showButton.addEventListener("click", async () => {
try {
const response = await fetch('fetch_car_things.php');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const tableContainer = document.getElementById("tableContainer");
if (data.length === 0) {
tableContainer.innerHTML = `<p>Tabellen är tom.</p>`
}
// Build the table HTML
let allTablesHTML = "";
Object.keys(data).forEach(tableName => {
let columns = [];
// Create header
let tableHTML = `<h2>${tableName}</h2><table><thead>`;
Object.keys(data[tableName][0]).forEach(colName => {
tableHTML += "<th>" + colName + "</th>";
columns.push(colName);
})
tableHTML += "</thead><tbody>";
// Create rows
data[tableName].forEach(row => {
tableHTML += "<tr>"
columns.forEach(col => {
tableHTML += "<td>" + row[col] + "</td>"
})
tableHTML += "</tr>"
})
tableHTML += '</tbody></table>';
allTablesHTML += tableHTML;
});
tableContainer.innerHTML = allTablesHTML;
} catch (error) {
console.error('Kunde inte hämta tabelldata:', error);
document.getElementById('tableContainer').innerHTML = '<p>Ett fel uppstod vid hämtning av data.</p>';
}
});
</script>
</body>
</html>
<?php $dbconn = null ?>