In my oop php guestbook I sucssed to add the button delete but in sql database I wrote the function to delete the comment but I don't know hor can I add it to the comment and the comment be deleted
How to add the Delete Button
I have task to create two guestbook , one with oop php and the second by sql
In my oop php guestbook I sucssed to add the button delete but in sql database I wrote the function to delete the comment but I don't know hor can I add it to the comment and the comment be deleted
Here in my oop code looks like the button and it shows in the picture.
foreach($messageList as $key =>$obj){
echo "<div class='div-containing-message'><h3>Posted by: " . $obj->getUsername() . "</h3><p>" . $obj->getMessage() . "</p><h4>Post's Date: " . $obj->getDateTime() . "</h4><br/><a class='guestbook-message-delete' href='part1.php?del=$key'>". "Delete</a></div><br/>";
}
in my sql database
delete function is in my Post.class.php:
public function deletePost(int $id) {
$sql = "DELETE FROM guestbook WHERE id=$id";
return $this->db->query($sql);
}
and my code of the sql guestbook is :
<?php $page_title = "Visitors's GuestBook"; ?>
<?php include("includes/header.php"); ?>
<?php include("includes/mainmenu.php"); ?>
<article class="mainContent">
<?php echo "<h1>$page_title</h1><br/>"; ?>
<article class="guestbook-container">
<div class="left-flexbox">
<?php
$db = new mysqli(DBHOST, DBUSER, DBPASS, DBDATABASE);
if($db->connect_errno > 0) {
die("connection error: " . $db->connect_error);
}
if(isset($_POST['user'])) {
$user = $_POST['user'];
$content = $_POST['content'];
$created = $_POST['created'];
$sql = "INSERT INTO guestbook(user, content, created)VALUES('$user', '$content', NOW())";
$result = $db->query($sql);
}
?>
<h3>Leave A Comment!</h3>
<form action="part3.php" method="post">
<label for="user">Your Name: </label>
<input type="text" name="user" id="user">
<br/>
<br/>
<label for="content"> Your Comment:</label>
<textarea class="textrutan-message" name="content" id="content" rows="10" cols="30"></textarea>
<br/><br/>
<input type="submit" name="postMessage" value="Sumbit Your Post"><br/>
</form>
</div>
<div class="right-flexbox">
<?php
// SQL-Question
$sql = "SELECT * from guestbook /*ORDER BY created DESC*/";
$result = $db->query($sql);
while($row = $result->fetch_assoc()) {
?>
<div class='div-containing-message'><h3> Posted By: <?= $row['user']; ?></h3>
<p> <?= $row['content']; ?></p>
<h4> Post's Date: <?= $row['created']; ?></h4>
</div>
<?php
}
?>
</div>
</article>
<?php include("includes/footer.php"); ?>
Step by step
Solved in 2 steps