This commit is contained in:
Dusk 2021-12-22 22:41:48 +01:00
parent 682d07e43a
commit 89087eeae2
5 changed files with 79 additions and 16 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
# Database
*.db

0
README.md Normal file → Executable file
View File

20
form.php Normal file → Executable file
View File

@ -1 +1,21 @@
<?php
include_once('persistence.php');
$db = createDB();
function newMessagePOST($db) {
if(isset($_POST['message']) && $_POST['message']) {
if(isset($_POST['name']) && $_POST['name']) {
$name = $_POST['name'];
$message = $_POST['message'];
storeMessage($db, $message);
}
else {
$message = $_POST['message'];
storeMessage($db, $message);
}
}
}
newMessagePOST($db);
header("Location: /index.php");
exit;

41
index.php Normal file → Executable file
View File

@ -1,34 +1,51 @@
<!DOCTYPE html>
<?php
require_once('persistence.php');
$db = createDB();
?>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Guestbook</title>
</head>
<body>
<form action="form.php" method="POST">
<label>Name:</label>
<input type="text" name="name"/><br/>
<textarea name="message" cols=50 rows=10 style="overflow:auto;"></textarea><br/>
<input type="submit" value="Submit"/><br/>
</form>
<?php
//storeMessage($db, 'Hola Dendy');
if(deleteMessage($db, 1)) {
echo('Success');
}
else {
echo('Failure');
}
//var_dump(storeUser($db, 'Dendy', 'Penisgros'));
//storeMessage($db, 'Content', 1);
//var_dump(userFromMessage($db, 2));
$test = getMessages($db);
foreach($test as $msg) {
//var_dump($msg);
echo('<div class="message">');
foreach($msg as $key => $value) {
echo('<div class="content">');
echo("$key: $value");
echo('</div>');
}
$usr = userFromMessage($db, $msg['id_msg']);
$username = $usr ? $usr['username'] : 'Anonymous';
echo($username);
echo('<br>');
echo('---');
echo('<br>');
echo($msg['content']);
echo('<br>');
echo('</div>');
echo('<br/>');
}
?>
</body>
</html>

32
persistence.php Normal file → Executable file
View File

@ -63,7 +63,7 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
return $ret;
}
function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) {
function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) : bool {
$query = 'INSERT INTO message(id_user, content, date)
VALUES (:id_user, :content, :date)';
@ -73,6 +73,18 @@ function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) {
$statement->bindParam(':content', $content);
$statement->bindParam(':date', $date);
$statement->execute();
return ($db->changes() > 0);
}
function storeUser(SQLite3 $db, string $username, string $password) : bool {
$query = 'INSERT INTO user(username, password)
VALUES (:username, :password)';
$statement = $db->prepare($query);
$statement->bindParam(':username', $username);
$statement->bindParam(':password', $password);
$statement->execute();
return ($db->changes() > 0);
}
function deleteMessage(SQLite3 $db, int $message_id) : int {
@ -82,9 +94,25 @@ function deleteMessage(SQLite3 $db, int $message_id) : int {
$statement = $db->prepare($query);
$statement->bindParam(':id_msg', $message_id);
$result = $statement->execute();
if ($result === false) {
if($result === false) {
return -1;
}
// Number of changed rows
return $db->changes();
}
function userFromMessage(SQLite3 $db, int $message_id) {
$query = 'SELECT user.id_user, user.username
FROM user, message
WHERE message.id_user = user.id_user
AND message.id_msg = :id_msg';
$statement = $db->prepare($query);
$statement->bindParam(':id_msg', $message_id);
$result = $statement->execute();
$array = $result->fetchArray();
if(!$array) return null;
return ['id' => $array['id_user'], 'username' => $array['username']];
}