I am writing a program. In this page there are contents: posts and assignments. I can give a comment to the posts. However, how can I access the related post id?
Here are my PHP codes to add the db.
JavaScript
x
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));
}
}
Here are my comments form. If a content is a post, the I can give a comment.
JavaScript
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
}
}}
Advertisement
Answer
Just include the post id in your form as a hidden element – I am guessing at what the actual database name for the id is here – postID
should be renamed to whatever your actual DB table field is
JavaScript
<?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
}?>
it will come through as $_POST['postID']
in your php script