What's new

Paano kaya maasign yun reply nya

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
problima ko po is kapag nag rereply ako nahahalo sya sa iba pareparehas na po yun reply ko sa lahat ng content gusto ko po kung anu lang nakaaasign dun sa content na yun at reply dun lang po yun magpapakita.

ito code ko

<?php
ob_start();
// include navigation with connection and session
include "../partials/newnav.php";

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Posted</title>
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet">
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet">
<script src="You do not have permission to view the full content of this post. Log in or register now."></script>
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="You do not have permission to view the full content of this post. Log in or register now." integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="You do not have permission to view the full content of this post. Log in or register now." integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
<script src="You do not have permission to view the full content of this post. Log in or register now."
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</head>
<body>
<?php
// insert to post database
if (isset($_POST['post'])) {
$page_content = $_POST['post_content']; //get the input text
$pagecontent = $_POST['postcontent']; //get the input title
$date_created = strtotime(date("Y-m-d h:i:sa")); //get the date and time
// prevent to ****ing sql injection
$slq = mysqli_prepare($con, "INSERT INTO post (comment,title, date_created, user_id) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($slq, "sssi", $page_content, $pagecontent, $date_created, $user_id);
mysqli_stmt_execute($slq);
header('location:postinsert.php');
exit();
ob_end_flush();
}
?>
<?php
// display query from post database
$post_query = mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_created as TimeSpent FROM post left join registration on registration.user_id = post.user_id order by comment_id DESC limit 1 ") or die(mysqli_error($con));
// loop
while ($post_row = mysqli_fetch_array($post_query)){
$id = $post_row['comment_id']; //get the comment_id
$uid = $post_row['user_id']; //get the user_id
$postedby = $post_row['username']; //get the username
?>
<!-- body of posted -->
<div class="container col-md-6 bg-body-tertiary border rounded-3">
<h5>Posted by: <a href="#" style= "margin-left:10px; text-decoration:none;"> <?php echo $postedby; ?></a></h5>
<!-- time interval -->
<?php
$days = floor($post_row['TimeSpent'] / (60 * 60 * 24));
$remainder = $post_row['TimeSpent'] % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0)
echo date('F d, Y - H:i:sa', $post_row['date_created']);
elseif($days == 0 && $hours == 0 && $minutes == 0)
echo "A few seconds ago";
elseif($days == 0 && $hours == 0)
echo $minutes.' minutes ago';
?>
<img src="../img/hour.png" alt= "hour logo" style = "width:20px";>
<h1><?php echo $post_row['comment']; ?></h1>
<?php }?>
</div>
<form method="post">
Comment:<br>
<input type="text" name="id" value="<?php echo $id; ?>">
<textarea name="comment_content" class="form-control" rows="2" cols="44" style="" placeholder=".........Type your comment here........" required></textarea><br>
<input type="submit" name="comment">
</form>
</div>
</br>
<?php

if (isset($_POST['comment'])){
$comment_content = $_POST['comment_content'];
$post_id=$_POST['id'];

mysqli_query($con,"INSERT INTO comment (comment,date_posted,user_id,comment_id) values ('$comment_content','".strtotime(date("Y-m-d h:i:sa"))."','$user_id','$post_id')") or die (mysqli_error());

}
?>
<?php

$comment_query = mysqli_query($con,"SELECT * ,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment inner JOIN registration on registration.user_id = comment.user_id where comment_id = '$id' AND comment_id = '$id_comment' order by comment.date_posted DESC") or die (mysqli_error());
while ($comment_row = mysqli_fetch_array($comment_query)){
$comment_id = $comment_row['id'];
$comment_by = $comment_row['username'];
?>

<br><div class="container col-md-6 bg-body-tertiary border rounded-3"><?php echo $comment_by; ?> - <?php echo $comment_row['comment']; ?>
<br>
<?php }?>


<div class="mt-2">
<?php
include "../partials/footer.php";
?>
</div>
</body>
</html>
 
Ang problema sa iyong code ay naaayon kaagad ang reply na ipinapadala mo sa lahat ng nilalaman sa pahina. Gusto mo lamang na ma-assign ang iyong reply sa tiyak na nilalaman na iyong sinasagot. Para maayos ang isyung ito, kailangan mong maipasa ang comment_id ng nilalaman na iyong sinasagot sa iyong form at gamitin ito sa pag-insert ng iyong reply sa tamang row ng database.

Una, kailangan mong baguhin ang form ng iyong reply upang isama ang comment_id ng nilalaman na iyong sasagutin. Maaring gawin ito sa pamamagitan ng pagdagdag ng isang input field na may pangalan na "comment_id" na naglalaman ng comment_id ng nilalaman na iyong sasagutin. Halimbawa:

HTML:
<form method="post">
    Comment:<br>
    <input type="hidden" name="comment_id" value="<?php echo $id; ?>">
    <textarea name="comment_content" class="form-control" rows="2" cols="44" placeholder=".........Type your comment here........" required></textarea><br>
    <input type="submit" name="comment">
</form>

Pagkatapos nito, kailangan mong baguhin ang query sa pag-insert ng iyong reply upang isama ang comment_id na iyong ipinasa mula sa form. Ito ang bahagi ng iyong code na kailangan baguhin:

PHP:
if (isset($_POST['comment'])){
    $comment_content = $_POST['comment_content'];
    $post_id = $_POST['comment_id'];
    
    mysqli_query($con, "INSERT INTO comment (comment, date_posted, user_id, comment_id) VALUES ('$comment_content', '".strtotime(date("Y-m-d h:i:sa"))."', '$user_id', '$post_id')") or die(mysqli_error());
}

Sa code na ito, ginagamit natin ang $post_id na ipinasa mula sa form upang maipasok ito bilang comment_id sa database.

Pagkatapos nito, para maipakita ang mga reply sa tamang nilalaman, kailangan mong baguhin ang query para lamang kunin ang mga reply na may katumbas na comment_id ng nilalaman na iyong sasagutin. Ito ang bahagi ng iyong code na kailangan baguhin:

PHP:
$comment_query = mysqli_query($con, "SELECT *, UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment INNER JOIN registration ON registration.user_id = comment.user_id WHERE comment_id = '$id' AND comment_id = '$id_comment' ORDER BY comment.date_posted DESC") or die(mysqli_error());

Sa code na ito, ginagamit natin ang $id na nakukuha mula sa loop ng pag-display ng mga nilalaman upang hanapin ang mga reply na may katumbas na comment_id.

Dapat mong isaalang-alang na ang iyong code ay may ilang mga isyu sa seguridad tulad ng SQL injection. Maaring gusto mong isaalang-alang ang paggamit ng prepared statements upang maprotektahan ang iyong database mula sa mga banta sa seguridad.
 

Similar threads

Back
Top