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 <?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> <!DOCTYPE html>
<?php <?php
require_once('persistence.php'); require_once('persistence.php');
$db = createDB(); $db = createDB();
?> ?>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<title>Guestbook</title> <title>Guestbook</title>
</head> </head>
<body> <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 <?php
//storeMessage($db, 'Hola Dendy');
if(deleteMessage($db, 1)) { //var_dump(storeUser($db, 'Dendy', 'Penisgros'));
echo('Success'); //storeMessage($db, 'Content', 1);
} //var_dump(userFromMessage($db, 2));
else {
echo('Failure');
}
$test = getMessages($db); $test = getMessages($db);
foreach($test as $msg) { foreach($test as $msg) {
//var_dump($msg);
echo('<div class="message">'); echo('<div class="message">');
foreach($msg as $key => $value) { $usr = userFromMessage($db, $msg['id_msg']);
echo('<div class="content">'); $username = $usr ? $usr['username'] : 'Anonymous';
echo("$key: $value");
echo('</div>'); echo($username);
} echo('<br>');
echo('---');
echo('<br>');
echo($msg['content']);
echo('<br>');
echo('</div>'); echo('</div>');
echo('<br/>'); echo('<br/>');
} }
?> ?>
</body> </body>
</html> </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; 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) $query = 'INSERT INTO message(id_user, content, date)
VALUES (: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(':content', $content);
$statement->bindParam(':date', $date); $statement->bindParam(':date', $date);
$statement->execute(); $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 { 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 = $db->prepare($query);
$statement->bindParam(':id_msg', $message_id); $statement->bindParam(':id_msg', $message_id);
$result = $statement->execute(); $result = $statement->execute();
if ($result === false) { if($result === false) {
return -1; return -1;
} }
// Number of changed rows // Number of changed rows
return $db->changes(); 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']];
}