1

我正在写一个程序。在此页面中有内容:帖子和作业。我可以对帖子发表评论。但是,如何访问相关的帖子 ID?

这是我添加数据库的 PHP 代码。

if (isset($_POST['submit']) ) {
  if (empty($_POST["comment"])) {
     echo "null comment";
  } else {
    $comment = $_POST["comment"];
  }
  date_default_timezone_set('Europe/Istanbul');
  $commentDate = date("Y-m-d h:i:sa");
  
  $stmt = $conn->prepare("INSERT INTO comment(postId,commentOwnerId,commentText,commentDate) VALUES (?,?,?,?)");
  
  if ($stmt != false ) {
    if($password != ""){
    $stmt->bind_param('ssss',$postId,$commentOwnerId,$comment,$commentDate);
    if($stmt->execute()){
      ?> <p class="success"><?php echo "added." ?></p> <?php
   }else{
      ?> <p class="fail"><?php echo " failed"; ?></p> <?php
   }
    $stmt->close();
  }} else {
    die('prepare() failed: ' . htmlspecialchars($conn->error));
  }
}

这是我的评论表格。如果内容是帖子,我可以发表评论。

if($contentRow['typeId'] == 1){ ?>
<form method="POST" >
  <div class="input-group mb-3">
  <input type="text" class="form-control" name="comment" placeholder="Give a comment" aria-label="Recipient's username" aria-describedby="button-addon2">
  <button class="btn btn-outline-secondary" name="submit" type="submit" id="button-addon2">Share</button>
</div>
</form> 
<?php
}
}} 
4

1 回答 1

1

只需在表单中包含帖子 ID 作为隐藏元素 - 我猜这里 id 的实际数据库名称是postID什么 - 应该重命名为您的实际数据库表字段

<?php
if($contentRow['typeId'] == 1){ ?>
<form method="POST" >
  <div class="input-group mb-3">
  <input type="text" class="form-control" name="comment" placeholder="Give a comment" aria-label="Recipient's username" aria-describedby="button-addon2">
  <input type='hidden' name='postID' value='<?php echo $contentRow['postID']?>' />
  <button class="btn btn-outline-secondary" name="submit" type="submit" id="button-addon2">Share</button>
</div>
</form> 
<?php
}?>

它会像$_POST['postID']你的 php 脚本一样通过

于 2021-05-28T18:37:08.190 回答