调用另一个isset函数中的isset函数



我将以示例的形式提问。事情是这样的;我有一个index.php,我所有的帖子都在那里,点击一篇帖子后,我会使用我点击的帖子的id进入post.php。在post.php中,我有一张所有帖子都可以使用的评论表单。现在的问题是我想得到一个帖子的具体评论。我知道我可以通过将来自index.php的post_id获取到数据库中的注释表中来实现这一点。所以,请老板们,我如何才能将post_id添加到我的评论表中。

这是我的代码

<?php include "includes/db.php";?>
<!-- Receiving My Comment Form -->
<?php 
if (isset($_POST['submit_comment'])) {
$name = $_POST['author'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$date = date('Y-m-d h-i-s');
$sql = "INSERT INTO comments (name, email, post, comment, date, status, 
post_comment_id) VALUES ('$name', '$email', 'I want to learn PHP', 
'$comment', '$date', 'unapprove', '$getcommentid')";
$run = mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CMS SYSTEM</title>
<link rel="stylesheet" href="../bootstrap/css/bootstrap.css">
<script src="../bootstrap/js/bootstrap.js"></script> 
<script src="../js/jquery.js"></script>
</head>
<body>
<?php include "includes/header.php"; ?>  
<div class="container">
<article class="row">
<section class="col-lg-8">  
<!-- Displaying Post coming from Get Super Global from Index.php -->
<?php 
if (isset($_GET['post_id'])) {
$sel_sql = "SELECT * FROM posts WHERE id = '$_GET[post_id]'";
$run_sql = mysqli_query($conn, $sel_sql);
while ($rows = mysqli_fetch_assoc($run_sql)) {
echo '
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-header">
<h2>'.$rows['title'].'</h2>
</div>
<div>
<img src="'.$rows['images'].'" width="50%">
<p>'.$rows['description'].'</p>
</div>
</div> 
</div>';
}
} else {
echo '<div class="alert alert-danger">No post you  selected to show: <a href="index.php">Click Here</a> to see more posts</div>';
}
?>
<hr>
<!-- Displaying Comments Here -->
<h2><u>Comment Section</u></h2>
<?php 
if (isset($_GET['post_id'])) {
$com_sql = "SELECT * FROM comments WHERE status = 'approved' AND 
post_comment_id = '$_GET[post_id]'";
$run_com = mysqli_query($conn, $com_sql);
while ($result = mysqli_fetch_assoc($run_com)) {
echo '
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-header">
<h2>'.$result['name'].'</h2>
</div>
<div>
<p>'.$result['date'].'</p>
<p>'.$result['comment'].'</p>
</div>
</div> 
</div>';
}
} else {
echo '<div class="alert alert-danger">No post you  selected to show: <a 
href="index.php">Click Here</a> to see more posts</div>';
}
?>
<!-- Comment Form -->
<div class="col-lg-10">
<div class="page-header"><h2>Leave A Comment Here</h2></div>
<div class="container-fluid">
<form class="form-horizontal col-lg-8" action="post.php" method="post">
<div class="form-group">
<label for="name" class="" required>Name</label>
<input id="name" type="text" class="form-control" name="author">
</div>
<div class="form-group">
<label for="email" class="" required>Your Email Address</label>
<input id="email" type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label for="comment" class="">Your Comment Here</label>
<textarea name="comment" id="comment" rows="5" cols="50" tabindex="4" required="required"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-block" name="submit_comment">
</div>
</form>
</div>
</div>   
</section>
<?php include 'includes/sidebar.php'; ?>    
</article>            
</div>
<div style="width:50px; height:50px;"></div>
<?php include 'includes/footer.php'; ?>
</body>
</html>

我没有测试这个,我只是把它写在这个评论框里,希望语法正确:(

<?php // index.php

$posts = {1,2,3,4,5};
foreach( $posts => $post ) {
echo "<a href='post.php?id=" . $post . "' >post number #" . $post . "</a><br>";
}
?>
<?php // post.php
if (isset( $_GET["id"] ) ) {
$st = $dbpdo->prepare("select * from posttable where id = :id");
$st->bindParam(':id', $_GET["id"], PDO::PARAM_INT);
$st->execute();
$result = $st->fetchAll();
echo "your comment is " .  $result[0]["COMMENT"];
}
?>

最新更新