Это видео недоступно.
Сожалеем об этом.
Show comment message from the database - PHP tutorial
HTML-код
- Опубликовано: 22 янв 2016
- Show comment message from the database - Learn PHP backend programming. Today we will display the comments we uploaded in the previous episode.
--
mmtuts is a RUclips channel that focuses on teaching beginner and advanced courses in various multimedia related skills.
We plan to make tutorials available on programming, video production, animation, graphic design, and on software such as the Adobe Creative Cloud programs.
PHP for beginners is a how to series that teaches the PHP coding language to people who are just starting out learning programming. The course teaches how PHP scripting can be made easy and teaches how to build many apps such as a login system, a comment section, how to upload images, how to create users in a website, and much more. Creating dynamic websites with PHP is easy and should not be seen as otherwise, which is why we want to explain the language in a easy to understand way for beginners.
If you have suggestions on new courses, or specific lessons within existing courses you would like to see, then feel welcome to submit them in the comment section or in a private message. ALL suggestions will be seen, but not all will be replied to since we get quite a few every day.
I didn't know what php was only a few weeks ago and I wanted to make a site where multiple user makes multiple inputs and they get result after "computer" analyze and returns it. "Computer" would be a server, now I know. What I learned from your tutorial taught it can be done with php and I got quite happy today. Thanks.
I want to thank you for the tutorial, my client wanted to add a comment system last minute to his website and this helped alot. However, now he wants to add a reply option to the comments that are posted. I wanted to know how I will be able to do that since I didnt see that option in the tutorial. thank you again for your help.
easy and nice explanation! thank you!
the only thing is missing I believe is when we comment it should get the new comments on top and older comments below.. (the order of comments should be new to older)
in your select statement, add ORDER BY uid DESC.
It works! Thanks
Interesting but can you tell me where to place it in that area or where it needs to be next to please thank you
I've been searching for this sooo long, thanks for your awesome tutorials and explanation!!
it seems like there are a very low ammount of php tutorials in comparison to other programming languages
I love your tutorials.. thank you very much.!. you're really great.. God Bless you.. :)
Thank you so much mtuts. I love your work... Well explained
Where's the next tutorial video after this video?
while(loop){
functions
}
exit(); # put this to avoid repeat display of data when you refresh page
}
Thank u for ur help mate, rly appreciate it, worked for me
you are welcome @@Leon-zo2wj
where are you from? @@Leon-zo2wj
Not working
where do you input string thou
I am learning a lot with your videos thanks man !
Nice one. Works really well. Keep up the work buddy.
text just going straight i already add .
Thankyou Dani for these great tuts
For everyone with issues about cloned comments after refreshing, do this:
Go to your comment.inc.php and index.php files and add ob_start(); right after
still not working....
this work great....thanks for the information
mmtuts, when ever I refresh my page it copy's my previews comment and pastes it so it creates a clone and in the data base aswell, help please.
me too wtf
i have the same problem
Does it ask you permission to resend?
If it does AND you click 'resend' it literally reruns the sql INSERT query. Instead click on the URL and press ENTER. That way the page is revisited and NOT rerun.
I don't know how to solve it yet, maybe by 'emtying' the $variable sql after running the query but I haven't tried that yet. Hope it helps.
I just used the php header command to send the user back to the same page after they submit the comment. This way, they technically aren't just reloading the page, but it has the same effects of reloading, except it doesn't resubmit the comment. Fixed. If you need help just reply and I'll help when I see it.
At the end of the setComments function add:
header("Location: ./index.php");
if you have this error: $row = $result->fectch_assoc();
Try this if php I think: $row = mysqli_fetch_assoc($result);
Yup
i see spelling error too
Life savior
now it is while($row=mysql_fetch_array or assoc($result)){}
hi can any of you help? i did exactly what he did but on my website it gives me this error:
Parse error: syntax error, unexpected 'getComments' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\website tester\index.php on line 31
this is my code if you want to check it out...
thanks in advance
comments.inc.php:
Great teacher
Thank you very much !
Great job. thank you so much sir
when i change the order of these 2 functions i get Call to undefined function getComments() error but there is nothing to cause this error between 2 functions
Thanks so much mate!! nice tutorial, really helped a lot
Hey, how come I can't call the getComments functon?
Call to undefined function getComments();
Notice: Undefined variable: result in C:\xampp\htdocs\commentsec\comments.inc.php on line 16
Fatal error: Call to a member function fetch_assoc() on null in C:\xampp\htdocs\commentsec\comments.inc.php on line 16
thats the error I am getting after i got the typo corrected, right now the getComments function is this
unction getComments($conn){
$sql = "SELECT * FROM comments ORDER BY cid DECS";
$results = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['message'];
}
also can you help in regards on how to stop the duplication of entries if and when i refresh the page, I did go through the comments below to check if others were facing the same issue, but was not able to understand how they solved it.
Thanks !!
I resolved it by changing a bunch of stuff, here's my comments.inc.php file:
Great tutorial, great accent
I just don't understand one thing here! The 'message' variable ($message = $_POST['message']) was declared inside the 'setComments' function, but how the 'getComments' function got access to it (which was outside of the 'setComments' function) with '$row['message']' & showing the result without any error?
Hey mmtuts, great video and it's helped me out quite a lot. So I'm trying to comment on various articles. So each comment has to relate to the specific article it's intended for. All articles are in db. My comments table has the articleID as well as comment,id,user. The problem I have is that I only seem to able to output the last comment submitted. Previous comments stay in db but only last one displays. Not sure if I have to do INNER JOIN or something? Any suggestions would be great. Thanks. Andy.
At 4:00, you wrote
$row = $result->fetch_assoc();
Is that good or should it be changed to
$row = mysqli_fetch_assoc($result);
like we have in the other tutorials?
You should change it
Thanks... works great.
from a forum "NEVER put $_POST or $_GET data directly into a query, it can be very harmful if someone seeks to exploit your mistake." - Is there another way to do this, than what you have been teaching us these last 7 episodes?
This one is not working on my wonderful creation. everytime i display the comments from my database inside a div, it will only display the latest. and it's insane.
Comment neither getting inserted into database nor showing up when writing through Android phone but everything works fine in my pc browser!!!Can anyone suggest me what's wrong?
sir??
can i have a question? why does my database comments go beyond the comment-box??
how to change the order of comment from top the latest ?...
The teaching is very good.But When I refresh the page , it will ask about a form resubmission.And when I click continue , The previous comment duplicates and displays
Is there a solution for it pls?
how can you make comment on a comment? , like what we have on yt comment here
I've been having the problem of comments being endlessly duplicated when I reload. I could use some help.
I'm facing a serious problem
When I'm reloading my comments that I've written before are saving into my database. Its happening if I leave my comment box empty and reload my page. What should I do now??
I have the same issue now, did u manage to figure it out?
My comments wont show up in myphpadmin, everything seems to be working, no errors, but the messeges dont show
got it working, ask me if you have the same problem :D
@@moogwilliams5071 hey man, could you tell me how you fixed it?
@@moogwilliams5071 Bruh I'm still stuck, do you have the fix?
@@adamjones6473 sorry, it's been a while and it's really late, I'll search for what it was and get back to you as soon as I find it 😁
@@moogwilliams5071 I think I found out what my problem was lol, it was just a small typo. I wasn't getting any errors so it was just a matter of stumbling across it.
Thanks so much teacher i have tell your channel all my friends.. please can you made videos for mvc 5 or phyton please if you can thank you so much a lot
I keep getting this error mysqli::query(): Couldn't fetch mysqli also getting it when i try to send a comment to my DB
Hey, iknow I´m really late to ask a question, but maby it will work :) i love you tutorial and everthing worked fine until the very last step:
despite the use of nl2br (and also nl2br(stripslashes) and nl2br(stripcslashes), the comments shown on the website exceeds the white text box. Only of i press Enter while writing the comment, it works. Do you have any idea what i did wrong?
Hiya mate, me again. Just wondering how I get it to post the users name with the comment as right now it post the work 'uid' not sure if i messed up somewhere. Thanks again for the tutorials
Please help man, Ik its a old video and all, but whenever I refresh page, the comments multiply, by 2. Please help.
great video... but how do you stop duplicate of data submitted on refresh
hello, my comments are dupplicating in the database as well as in the front page..how do I solve that?
same
thanks for the tutorial ....Could you please provide these php files link too in your tut description ????
Almost everything works. I can insert comments into the database but it doesn't echo out the comments? Could you help?
awesome man
setComments doesn't work ? when I use it, nothing is printed
Hey buddy is this on the background or we need to refresh the page to display it
which one is the previous episode before this?
mmtuts do you have any mistake? i can't see the annotations anymore please help me
hi, I have this error:Uncaught Error: Call to a member function fetch_assoc() on bool in comments.inc.php: while ($row = $result->fetch_assoc()) {
and index.php: getComments($conn);
what could this be? I have checked for typos
i have the same problem. did you find the solution?
@@stellathemistokleous8071 no
I resolved it, at least for myself! here's my entire comments.inc.php file:
It worked until i got up to echoing out the results onto my page and then it wouldnt upload any comments
So currently if one were to put a single quote ' in the comment box it will not post. Been looking into prepared statements for this anyone else solve this issue so far though?
If you Write $con variable as parametter inside the set comments and Get comments function How does it kmow Where to get the Con variable from because he dident include the file
hi here is my entire comments.inc.php file which corrects these errors and worked for me:
I certainly need to hear an answer from you Sir. Wonderful tutorial, when I practised on my machine, in my localhost, I deleted all the comments from the database, but every time I use the url to open it up, the comments keep on multiplying in the table (database). Even if I refresh the browser, the same comment gets added in the table again WHY? Can you please help me if you know why this is happening? WAITING FOR YOUR REPLY SIR. Need it please let me know.
The reason that happens is because when we refresh the screen you're basically turning the URL to repost to its last comment displayed even if you delete everything in the database it will still show up because the database needs to be dropped and reinstall but that will only clear the screen of comments once you enter any other comments it will be listed in the database and then displayed on the comment section if you refresh it after that he will display it once again it's not a common session like Instagram or RUclips it's a common section for one particular picture video or display for the user to comment on
so I think that's how that works but I am now 100% sure that's just what I notice for myself when experimenting with this particular code entry
Love your tutorials so much man, I have a question though. How does RUclips make it so when you post a comment you see it pop up on the screen but the page doesn't visibly reload?
Each time a website interacts with a database it needs to refresh the page somehow, like when we post comments on RUclips.
I'm not sure which method exactly RUclips uses, but one of the ways to interact with databases WITHOUT needing to refresh the entire page, is to use Ajax. What Ajax does, is to only refresh part of the page, meaning that we can reload a single HTML element in our page (which contain all the comments under a video on RUclips as an example) without reloading the entire page.
Ajax is a method where you use PHP and JavaScript (or jQuery), in order to refresh specific content without reloading a page.
Here is a link: www.w3schools.com/js/js_ajax_intro.asp
when I refresh the browser my comments are cloning. I'm writing for example: qwerty and when I refresh I have 2 comments qwerty. PLS help.
Does it ask you permission to resend?
If it does AND you click 'resend' it literally reruns the sql INSERT
query. Instead click on the URL and press ENTER. That way the page is
revisited and NOT rerun.
I don't know how to solve it yet, maybe by 'emtying' the $variable sql
after running the query but I haven't tried that yet. Hope it helps.
I just used the php header command to send the user back to the same page after they submit the comment. This way, they technically aren't just reloading the page, but it has the same effects of reloading, except it doesn't resubmit the comment. Fixed. If you need help just reply and I'll help when I see it.
I still cannot get this fixed. Can you create a codepen for this? Also, i'm trying to submit comments by using AJAX. I don't want to reload the page every time a comment is typed.
I used the same technique but it shows an error " Cannot modify header information - headers already sent by.... "
hey man can you help me out please on how do i record time on database for example
am_login
am_logout
pm_login
pm_logout
thanks cos im trying to make a daily time record and im only new in programming and i dont know what to do
I keep getting "Uncaught Error: Call to a member function query() on null in ...coments.inc.php: 10 Stack trace: #0.... setComments(NULL)#1main thrown in... on line 10 - Any ideas?
Add this after form:
require 'comments.inc.php';
getcomments($conn);
anyone notice that the box won't accept apostrophe's? how would one fix this?
How can I show different comments on different pages? If anybody knows please help me!
Guys i have a problem. I haven't got any error but database doesn't read my comments.
if
echo nl2br($row['messagel']);
doset work, try
echo nl2br(stripslashes($row['messagel']));
or
echo nl2br(stripsclashes($row['messagel']));
Hi there, great tutorilas for beginners. I was wondering how can you protect your comment section from html tags?
Example: if i write comment like this some comments it will write a bold comment or even worse maybe someone could insert malicious code through our comment form.How to protect our comment form?
A few episodes ahead of this one I show exactly that ;)
Founded, thanks for express answer
What episode is that? I cant find it?
ruclips.net/video/Qt9W6U3msOk/видео.html
Hi this link shows nothing. Please check it. BTW I am unable to call the get_comments function.
it wont show the comments below..
Hi mmtuts, i did not use the tag, but why isnt my body tag in css change the background-color:#ddd ?
I'm also having the same issue, did anyone ever find the fix?
I cant get it to take anything from the comment database but I can put things in it
Hi ,mmtuts, I have a question. You used php+mysql to make a comment section. There is another way to make a comment section ,that is JavaScript+Ajax. Do you know the difference between these two methods? Thank you ,I am just a little confused about this.
Hi there :) Using JavaScript and Ajax isn't really another way of making a comment section, but rather a "upgraded" way of doing it. Ajax is a combination of using JavaScript and PHP in a way that doesn't require the user to refresh the browser each time they (as an example) post something in the comment section. It is simply a way to run PHP code on "part" of the website, whenever you interact with a database which allow for a smoother looking experience.
Now I know that ,thank you.Are you willing to do a AJAX tutorial?
I plan to do it in the future :)
I can't wait for your new tutorials!Thank you for all the tutorials you have done!
Not sure if your still replying but I'm wondering how to change it from showing the oldest comments at the very top to the newest ones, I've googled a bit and can't seem to find what I'm looking for but I can guess its got to do with the cid number etc just not sure how to write it.
To do this you need to change the select statement in getComments() from "SELECT * FROM comments" to "SELECT * FROM comments ORDER BY cid ASC"
everything works great but when i upload comments for my database info
:Anonymous2019-09-19 18:23:18message" not the comments input...can you help me make the correction
Hi again
just another question on this tutorial as i am learning php, in the comments.inc.php file should we always close off our php tags as i noticed its not
If your document ONLY contains PHP code, then it is better to not close the PHP tags at the end because it doesn't have a purpose. Only close the PHP tag if you have another type of code coming right after.
Ok cool thankyou very much i wasnt aware of this
Hi, I would like to ask if you know where is the problem in my code, I would like to change the date format to dd-mm-yy. So my code looked like .date('d-m-Y H:i:s'). but when i hit the submit button, the message doesn't even save to my database.
The SQL DATETIME format NEEDS to have the format as YYYY-MM-DD HH:MI:SS when you insert it into the database. It is a standard you can't change with DATETIME. If you want to change the format to dd-mm-yy, then you need to do it when you pull the data out from the database instead.
Here is a link that shows some of the different SQL date formats: www.w3schools.com/sql/sql_dates.asp
Thank you very much for a quick respond!
how to make "show more" comment button... i want to limit the comments to show 10comments until i click "show more" button.
My text is spilling out of the white background we set for individual comments. Thoughts as to why?
Thanks,
Wes
Nevermind. Figured it out. Great tutorial!
what was the problem?
How did you did it?
everything works. I can insert comments into the database but it doesn't echo out the comments?
i have the same problem
"i'm new in here"
this kind of comment is not acceptable in here .. what can i do for that ? i mean for a ( ' ) sign
Hi mmtuts, How do I make it so that a new comment gets added on top not at the bottom of all the comments?
use sql statement "order by" keyword ,
$sql = "SELECT * FROM comments order by cid desc";
Any one had this problem? - once the comment as been made if its a long one it spills over the comment box! I know its stated about 3mths ago but he didn't say how he fixed it.
Any help here would be great. Thanks.
Sorted I needed to type a sentence. Not just random letters to test it....lol
thanks alot , i have problem that each time i refresh the page the last comment is copied again to the data base ,
header("Location: index.php");
exit();--- i add this to the setComments($conn) function and now it is workiing
Why the select statement of "SELECT * FROM comments" doesn't selects all the database instead of the first row?
It does select all database results, however in order to get them from your SQL code to the website, you need to loop the results and spit them out individually using PHP.
First of all thank you for the quick answer!
Second of all, the "mysqli_fetch_assoc" spits only the top row that wasn't already selected?
Hey fyr, måske kunne du lave et CMS SYSTEM :D
How Am I able to make the newest comments first?
Do like this: $sql = "SELECT * FROM comments ORDER BY date DESC";
where did you put that though?
hello i have this error Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\php\KitchenStories\comments.inc.php on line 17. can you help me?
I changed a bunch of stuff in my comments.inc.php file and it resolve the fetch_assoc issue, here's my entire file:
Does anyone else have CSS not applying to Chrome, but works on IE ? Also.... when I refresh browser, I get to see a pop up saying "confirm form resubmission....", and I'm curious what part of the code is calling this.
Strangely, Chrome did update after a while.
In Chrome right click and Go to Inspect element>Network>check Disable Cache.
I used your code for posting updates. What if I want to display only 3 updates and not all the updates in the database? I 'm sorry for my question, I'm not that good at coding actually.
Just add LIMIT 3 at the end of the SQL select statement ;)
hahaha. Thank you :)
Can someone tell me what this fatal error means??
Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\comment_section\comments.inc.php on line 20
yes! here's my entire comments.inc.php file:
Hello :)
I changed your code to from $row = $result->fetch_assoc(); which gave me a fatal error message to $row = mysqli_fetch_assoc($result); and got a warning message instead:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/comments/comments.inc.php on line 17
Could you please tell me how to fix this problem?
Oh wait nevermind I got it... I thought your SELECT * FROM was SELECT + FROM... fixed it
Great tutorials by the way!
@Filmon Az Hello, i copied your code, but still facing error: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in. Help please, idk what to do
@Filmon Az ахаха, по уровню английского понял?) Я уже нашел ошибку, переменная conn была использоваан дважды. Спасибо за отзывчивость))
in 2024 this worked for me, here's my entire comments.inc.php file:
How can I show the newest comment on top of the website?
$sql =" SELECT * FROM posts ORDER BY ID DESC"
what is the use of -> operator can you explain it simply
It is OOP (Object Oriented Programming). The variable $conn is an instance of a class and the -> (arrow) is pointing to a method (function) that is inside the class that $conn is of.
Hope I got it right.
Here is the mysqli class:
php.net/manual/en/class.mysqli.php
How do you display multiple videos with comments boxes under each videos?
You just need to loop out the videos with one of the loops we have learned about. Then just include the or video tag inside the loop.
If you are using s then make sure you include the link and video number in the database.
if you are gonna use videos that are uploaded to your server then I would name each video the following:
vid-category-number.mp4
where you change category and number depending on your need.
Does that go for the comment box too or do we have to make individual tables for each of the comment boxes?
+Denny Feng
You need to make a separate table for the videos, but also include 2 new columns to the comments table that says which video category and video number it belongs to.
mmtuts
Hey mm, don't know how fast you can answer this, but for me when I have the second function in my php file, it suddenly stops working (?) I can setComments, but I cannot getComments, as whenever I try to actually have that function inside my PHP file, my website just doesn't want to open anymore (?)
my code: function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn, $sql);
$row = $result->fetch_assoc();
echo "$row['c_message']";
}
Deezee It sounds like you forgot to close a statement or a line of code in your setComments.
Deezee I noticed you haven't fixed the corrections I placed in the video annotation. Try turning on the annotations and watch the part where we create the setComments function again :)
Ok, thank you :D
Rewatched it , but couldn't find the annotation (?)
Can you tell me how to view newly posted comment on top.
Include "ORDER BY id ASC" at the end of your SQL statement.
Thanks..
After refresh the Index page the comment showing one more time .
Modify your setComment function like this:
function setComment($con) {
if(isset($_POST['commentSubmit'])) {
$user_id = $_POST['email'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (user_id, date, message)
VALUES ('$user_id', '$date', '$message')";
$result = mysqli_query($con, $sql);
header("Location: dashboard.php");
exit();
}
}
AT the end of which SQL statement? I tried it in the SELECT statement inside the comments.inc.php, but it just messed up my code.
When I refresh the website, the comment also refresh and it displays twice. So why ?
You need to add exit(); at the end of your php script after the header(); to make sure it closes the script afterwards. I forgot to ad it in this video, but I mention it in a later episode.
Haha.. Thanks very much from your quick response and your great tutorials.
And thank you for watching :)
In which tutorial you have add
Modify your setComment function like this:
function setComment($con) {
if(isset($_POST['commentSubmit'])) {
$user_id = $_POST['email'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (user_id, date, message)
VALUES ('$user_id', '$date', '$message')";
$result = mysqli_query($con, $sql);
header("Location: dashboard.php");
exit();
}
}
Fatal error: Call to a member function fetch_assoc() on null
It means that your SQL statement doesn't get any results from the database. So there is an error in your SQL code. Very likely because of a typo.
the OP made a few syntax errors, here's my entire comments.inc.php file which resolves the problems:
Please helppp mee
Fatal error: Uncaught Error: Call to undefined function getComments() in C:\xampp\htdocs\wow\second3.php:327 Stack trace: #0 {main} thrown in C:\xampp\htdocs\wow\second3.php on line 327
Try turning it off and on again
I resolved it, here's my entire comments.inc.php file:
How can I refresh the comments without reloading the website?
You should look into AJAX :)
mmtuts Oh, Thanks!
but how can I show the newest comment on top of my website?
the function where you selected from database just change to $sql =" SELECT * FROM posts ORDER BY ID DESC"
where did $conn came from?
It comes from the connection we made in the dbh.inc.php file in a previous episode.
what episode was it? reply please
The one where we start learning about login systems a few episodes back.
Source code?
too bad you formatted the $result variable wrong and never explained your error.