Click here to Skip to main content
15,918,343 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
<form action="" method="GET">
<?php
$sql = mysqli_query($con, "select * from blogs");
while($row = mysqli_fetch_array($sql)){
echo  '<img id="myImg" style="height: 400px; width: 800px; float: left;" src="pages/'.$row['url'].'" alt="'.$row['description']. '">';
echo '<br />';
echo '<p >'.$row['description']. '</p>';
echo '<br />';
echo '<br />';
echo '<br />';
$del = $row['id'];
$sql1 = mysqli_query($con, "SELECT * FROM blog_comments WHERE id= '$del'");
while($row1 = mysqli_fetch_array($sql1)){
echo '<p>   Posted by: ' .$row1['username'] .'<p>';
echo '<p>     ' .$row1['comment'] .'<p>';
echo "<th><a href=\"blog.php?delete={$row1['user_id']}\">delete</a></th>";
echo '<br />';
}**echo '<form method="GET">';
echo '<input type="hidden" name="id" value="'.$row['id'].'">';
echo '<h2>Comments</h2>';
echo '<label>Username</label><br />';
echo '<input type="text" name="username">';
echo '<br />';
echo '<label>Comment :</label><br />';
echo '<textarea name="comment" id="comment" width="300px"></textarea><br />';
echo '<button name="submit1">Submit</button>';
echo '</form>';
}
}
?>
</form>


What I have tried:

Comment of each post using php. Hi i need help ? i just want to insert a comment on each post just like on facebook but i have problem on my code. im hardly analyzing code.. the process is the comment only it insert into the last post of my blog. is anybody knows how to insert comments into the desire post that i want to comment
Posted
Updated 19-Sep-22 7:11am
v2
Comments
Member 15627495 4-Oct-22 10:33am    
line 7 : you wrote "<p >" , there is one space to fix.



when you use 'ECHO' you put in buffer your datas, before the server send the page to the client.
why don't you use one var to have a stamp/buffer ?

while($row = mysqli_fetch_array($sql)){
$stamp = '<img id="myImg" style="height: 400px; width: 800px; float: left;" src="pages/'.$row['url'].'" alt="'.$row['description']. '">';
$stamp .= '<br><p >'.$row['description']. '</p><br><br><br>';
//............
// echo $stamp; // as final command


<br /> is equal to <br> when display.
It's Xhtml against Html
a simple <br> will make the job perfectly


and for your sql query : $sql1 / $sql2 , use a better naming for them.( or you will lost yourself the day you'll have 30 query called sql1 in your code.... )
you can be explicit :
$sql_get_all_from_blogs = 'select * from blogs';
$sql_get_all_from_comments_for_one_user_id = 'select * from comment where user='.$id;
Richard Deeming 6-Oct-22 5:42am    
Aside from the fact that the OP probably isn't still looking for an answer five years later, your last code block is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

1 solution

PHP
$sql1 = mysqli_query($con, "SELECT * FROM blog_comments WHERE id= '$del'");

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900