gpt4 book ai didi

php - 使用PHP从数据库中删除评论

转载 作者:行者123 更新时间:2023-11-29 13:19:36 25 4
gpt4 key购买 nike

我的网站是管理员登录网站,因此用户无法注册。不过,他们可以在网站上发表评论。我在评论上有删除按钮,但没有意识到任何人都可以删除任何人的评论。我如何更改此设置,以便当管理员登录时,他们能够删除不适当的评论(如果有)并从公众中删除删除按钮。

这是我的 Comment.php 代码,其中包含删除按钮功能:

   <?php 

session_start();
require_once 'templates/open.php';
require_once 'connect.php';
require_once 'functions/cleanstring.php';
require_once 'functions/encrypt.php';

?>

另一个代码文件:

   <?php
$db_hostname = 'localhost';
$db_database = 'cs12e2g_MyFirstDB'; //'Your database name'
$db_username = 'cs12e2g_DBuser'; //'your username';
$db_password = 'vtjppqs7'; //'Your password';
$db_status = 'not initialised';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
$db_status = "connected";
if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_connect_error());
$db_status = "not connected";
}


// Includes and variables always required

require_once 'recaptcha/recaptchalib.php';
require_once 'functions/cleanstring.php';
$privatekey = "6Lem4-gSAAAAADsaa9KXlzSAhLs8Ztp83Lt-x1kn";
$publickey = "6Lem4-gSAAAAAMHLAVbieIknMtUZo71ZKzzCkoFN";
mysqli_select_db($db_server, $db_database);

$str_message = "";
if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_connect_error());
}else{

if(isset($_GET['delete'])){
$deleteq="DELETE FROM comments WHERE ID={$_GET['delete']} LIMIT 1";

$deleter=mysqli_query($db_server, $deleteq);
IF($deleter){
echo"<p>That message was deleted!</p>";}}




//Test whether form has been submitted
if(trim($_POST['submit']) == "Submit"){
//Handle submission
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
$str_message = "The reCAPTCHA wasn't entered correctly. Go back and try it
again.
(reCAPTCHA said: " . $resp->error . ")";
} else {

// Your code here to handle a successful verification
$comment = $_POST['comment'];
if($comment != ""){
$query = "INSERT INTO comments (comment) VALUES ('$comment')";
mysqli_query($db_server, $query) or die("Comment insert failed: " .
mysqli_error($db_server) );
$str_message = "Thanks for your comment!";
}else{
$str_message = "Invalid form submission";
}
}
}
//Create page with or without submission
$query = "SELECT * FROM comments";
$result = mysqli_query($db_server, $query);
if (!$result) die("Database access failed: " . mysqli_error($db_server) );
while($row = mysqli_fetch_array($result)){
$ID= $row['ID'];
$str_result .= "<p><em>Comment $j (" . $row['commDate'] .
")</em><br /> " .$row['comment'] . "</p>
<a href ='commentnow.php?delete=$ID
'>Delete</a><hr />";
}
mysqli_free_result($result);
}

?>




<h1>What do you think?</h1>


<p><h5>Did you find everything you wanted? Please comment below:<h5></p>

<form action="commentnow.php" method="post">
<textarea rows="10" cols="50" name="comment"></textarea><br />
<?php echo recaptcha_get_html($publickey); ?>
<input type="submit" name="submit" value="Submit" />
</form>
<span style="color:#FF0000;">
<?php echo $str_message; ?></span>
<hr />
<h2>Comments:</h2>
<?php echo $str_result; ?>




</div>

<?php
require_once 'templates/close.php';


?>

我有一个members.php页面,对应于管理员登录的时间(他们是唯一可以访问此页面的人),删除按钮代码是否必须放在这里?那么他们是唯一可以使用该功能的人吗?如果是的话,它会去哪里,如何去?

最佳答案

限制删除按钮仅对管理员显示。此外,这还意味着您可以以某种方式识别登录用户是否是管理员。

if ($is_admin) {
// Code to display button
}

同时在后台检查登录用户是否为admin

if ($is_admin) {
// Code to delete comment
delete_comment();
}

关于php - 使用PHP从数据库中删除评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com