Message Deletion
This commit is contained in:
parent
7f02744efe
commit
6b2b17b74e
20
index.php
20
index.php
|
@ -3,24 +3,32 @@
|
||||||
require_once('persistence.php');
|
require_once('persistence.php');
|
||||||
$db = createDB();
|
$db = createDB();
|
||||||
?>
|
?>
|
||||||
<html lang="ca">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<title>Guestbook</title>
|
<title>Guestbook</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<table border="1">
|
|
||||||
<?php
|
<?php
|
||||||
//storeMessage($db, 'Hola Dendy');
|
//storeMessage($db, 'Hola Dendy');
|
||||||
|
if(deleteMessage($db, 1)) {
|
||||||
|
echo('Success');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
echo('Failure');
|
||||||
|
}
|
||||||
$test = getMessages($db);
|
$test = getMessages($db);
|
||||||
foreach($test as $msg) {
|
foreach($test as $msg) {
|
||||||
echo('<tr>');
|
echo('<div class="message">');
|
||||||
foreach($msg as $key => $value) {
|
foreach($msg as $key => $value) {
|
||||||
echo("<td>$key: $value</td>");
|
echo('<div class="content">');
|
||||||
|
echo("$key: $value");
|
||||||
|
echo('</div>');
|
||||||
}
|
}
|
||||||
echo('</tr>');
|
echo('</div>');
|
||||||
|
echo('<br/>');
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
</table>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
||||||
// Can't create table if it already exists
|
// Can't create table if it already exists
|
||||||
$exists = file_exists($db_name);
|
$exists = file_exists($db_name);
|
||||||
|
|
||||||
$db = new SQLite3($db_name);
|
$db = new SQLite3($db_name);
|
||||||
|
|
||||||
if(!$exists) {
|
if(!$exists) {
|
||||||
|
@ -43,7 +42,6 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
||||||
// Prepare statement
|
// Prepare statement
|
||||||
$query = 'SELECT * FROM message' . $append_string;
|
$query = 'SELECT * FROM message' . $append_string;
|
||||||
//var_dump($query);
|
//var_dump($query);
|
||||||
|
|
||||||
$statement = $db->prepare($query);
|
$statement = $db->prepare($query);
|
||||||
|
|
||||||
foreach($filter as $key => $value) {
|
foreach($filter as $key => $value) {
|
||||||
|
@ -51,7 +49,6 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $statement->execute();
|
$result = $statement->execute();
|
||||||
|
|
||||||
$notnull = true;
|
$notnull = true;
|
||||||
$ret = [];
|
$ret = [];
|
||||||
while($notnull) {
|
while($notnull) {
|
||||||
|
@ -67,7 +64,9 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) {
|
function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) {
|
||||||
$query = 'INSERT INTO message(id_user, content, date) VALUES (:id_user, :content, :date)';
|
$query = 'INSERT INTO message(id_user, content, date)
|
||||||
|
VALUES (:id_user, :content, :date)';
|
||||||
|
|
||||||
$date = (new DateTime('now'))->getTimestamp();
|
$date = (new DateTime('now'))->getTimestamp();
|
||||||
$statement = $db->prepare($query);
|
$statement = $db->prepare($query);
|
||||||
$statement->bindParam(':id_user', $user_id);
|
$statement->bindParam(':id_user', $user_id);
|
||||||
|
@ -75,3 +74,17 @@ function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) {
|
||||||
$statement->bindParam(':date', $date);
|
$statement->bindParam(':date', $date);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteMessage(SQLite3 $db, int $message_id) : int {
|
||||||
|
$query = 'DELETE FROM message
|
||||||
|
WHERE id_msg = :id_msg';
|
||||||
|
|
||||||
|
$statement = $db->prepare($query);
|
||||||
|
$statement->bindParam(':id_msg', $message_id);
|
||||||
|
$result = $statement->execute();
|
||||||
|
if ($result === false) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// Number of changed rows
|
||||||
|
return $db->changes();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue