Show sourcecode
The following files exists in this folder. Click to view.
ramverket/exercises/mysql-intro/
ovn_sqlintr1.php
ovn_sqlintr2.php
ovn_sqlintr2.php
181 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
<?php
// Title: MySQL Intro 2
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
?>
<?php include("../../incl/connect_db.php"); ?>
<?php include("../../incl/db_handler.php"); ?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,300..900;1,300..900&display=swap" rel="stylesheet">
<title>MySQL Intro 2</title>
</head>
<body>
<?php
$table = "users";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['delete_all'])) {
deleteAllTableRows($conn);
}
if (isset($_POST['insert_row'])) {
insertTableRow($conn, $table, $_POST['first_name'], $_POST['last_name'], $_POST['mobile'], $_POST['email']);
}
if (isset($_POST['delete_row'])) {
deleteTableRow($conn, $table, $_POST['id']);
}
if (isset($_POST['update_row'])) {
updateTableRow($conn, $table, $_POST['id'], $_POST['first_name'], $_POST['last_name'], $_POST['mobile'], $_POST['email']);
}
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$login = login($conn, $table, $username, $password);
if ($login[0]) {
setcookie("id", $login[1], time() + (86400 * 30), "/");
} else {
echo "Fel användarnamn eller lösenord";
}
}
if (isset($_POST['logout'])) {
setcookie("id", "", time() - 3600, "/");
}
if (isset($_POST['register'])) {
$id = insertTableRow($conn, $table, "username, password, first_name, last_name, mobile, email, type", $_POST['username'], $_POST['password'], $_POST['first_name'], $_POST['last_name'], $_POST['mobile'], $_POST['email'], "user");
setcookie("id", $id, time() + (86400 * 30), "/");
}
if (!isset($_POST['to_register'])) {
header("Location: ovn_sqlintr2.php");
exit();
}
}
if (!tableExists($conn, $table)) {
createTable($conn, $table);
}
?>
<?php if (isset($_COOKIE['id'])): ?>
<?php
$userStmt = getTableRow($conn, $table, "id", $_COOKIE['id']);
$user = $userStmt->fetch(PDO::FETCH_ASSOC);
if (!$user) {
setcookie("id", "", time() - 3600, "/");
header("Location: ovn_sqlintr2.php");
exit();
}
?>
<h1>Hej <?php echo $user['first_name']; ?>!</h1>
<form action="" method="POST"><input type="submit" name="logout" value="Logga ut"></form>
<?php if ($user['type'] === "admin"): ?>
<table>
<tr>
<th>ID</th>
<th>Användarnamn</th>
<th>Lösenord</th>
<th>Förnamn</th>
<th>Efternamn</th>
<th>Telefonnummer</th>
<th>E-post</th>
<th>Typ</th>
</tr>
<?php foreach (getAllTableRows($conn, $table) as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['type']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php else: ?>
<?php if (isset($_POST['to_register'])): ?>
<form action="" method="POST">
<input type="text" name="username" placeholder="Användarnamn" required>
<input type="password" name="password" placeholder="Lösenord" required>
<input type="text" name="first_name" placeholder="Förnamn" required>
<input type="text" name="last_name" placeholder="Efternamn" required>
<input type="number" name="mobile" placeholder="Telefonnummer">
<input type="email" name="email" placeholder="E-post">
<input type="submit" name="register" value="Skapa konto">
</form>
<?php return ?>
<?php endif; ?>
<form action="" method="POST">
<input type="text" name="username" placeholder="Användarnamn">
<input type="password" name="password" placeholder="Lösenord">
<input type="submit" name="login" value="Logga in">
</form>
<form action="" method="POST">
<input type="submit" name="to_register" value="Skapa konto">
</form>
<?php endif; ?>
<style>
* {
font-family: "Rubik", sans-serif;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
gap: 20px;
}
table {
border-collapse: collapse;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
margin: 10px 10px;
box-sizing: border-box;
}
th:first-child {
border-radius: 10px 10px 0 0;
}
tr {
background-color: white;
}
tr:nth-child(even) {
background-color: #EBEBEB;
}
td, th {
text-align: start;
padding: 7px 30px 7px 13px;
text-align: start;
margin: 0 10px;
font-weight: 400;
}
th {
padding: 15px 30px 15px 15px;
font-weight: 600;
}
</style>
</body>
</html>