Loading...

Insert message from comment system into database - PHP tutorial

Insert message from comment system into database - PHP tutorial
Insert message from comment system into database - Learn PHP backend programming. Today we will insert comments into our database directly from the website.

At 15:20 I made a mistake in this video by accidentically combining Procedural PHP with Object Oriented PHP.
Please don't turn off "annotations" so you can see the "fix" for what you need to write instead once I get to it in the video.

--

mmtuts is a YouTube 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.

RRelated Posts

3 Simple Life Hacks

3 Simple Life Hacks FOLLOW ME ON SOCIAL MEDIA FACEBOOK : https://www.facebook.com/superviral1 PINTEREST : https://www.pinterest.com/salascar1/ TWITTER : www.twitter.com/salascar1/ Music; 1.

8 Websites Too Creepy to Visit

You'd be surprised at how many websites you can find that aren't on the dark web (or deep web). Here are eight scary sites you shouldn't visit. CHECK THIS ONE OUT TOO:

CComments

  • Rishabh Gupta
    thanks
  • buba conteh
    Hi . I hav all d backend codes for d comment section but in my browser it displays dat it cant find my database.which I have correctly done.
  • panos paou
    i was trying for hours to see why on page refresh page comments appears again
    header("Location: ".$_SERVER['REQUEST_URI']);
    exit();
    i used this inside isset for button submit and all fine
  • Zareh Badkerhanian
    Why does the form have to be echoed? Can someone please explain
  • D P
    <?php
    echo "Was looking for so long and finally .... Thanks";
    ?>
  • himanshu
    thank you so much correcting error. man, I will support you by donating
  • Himanshu Armstrong
    error- call to undefined setcomments()
    your code never works! ughhh
  • Static Void
    <?php
    function setComments($conn) {
    if (isset($_POST['commentSubmit'])) {
    include_once 'dbh.inc.php';
    $uid=$_POST['uid'];
    $date=$_POST['date'];
    $message=$_POST ['message'];

    $sql= "INSERT INTO comments (uid, date, message)
    VALUES ('$uid', '$date', '$message')";
    $result= mysqli_query($conn, $sql);
    }
    }

    this code keeps giving me this errors:
    Notice: Undefined index: uid in C:\laragon\www\6\comments.inc.php on line 5

    Notice: Undefined index: dat in C:\laragon\www\6\comments.inc.php on line 6

    Can you tell me why?
  • E Jeffery
    This works well. Only issue, is that when the page is refreshed, the comments duplicate in the database.
  • Pizza Squeeze
    I tried sending the message to the database but the message did not go to the database can i have some help please
  • Motaz Alnassan
    you are the best
  • GamerGriffin
    Hey mmtuts, I just have a simple question that I was hoping you could answer. What do you mean by you mixed up Procedural PHP with OOP PHP? What's the difference?

    If you can't provide a brief answer, then I'm happy to look up the answer in my own time, but if you could that would be great.
  • ashu rawat
    it adds only 1 comment after that nothing changes in my database after clicking submit .
  • reynald cruz
    sir, my date and database only shows 0000-00-00 00:00, how could i fix it? i'm using Asia/Manila
  • Omid Shagiwal
    hello sir, when i press the comment button phpmyadmin is not receive data, please help me out....
  • Miguel Bryan Juteau
    HELP!

    "Connection failed: Can't connect to MySQL server on 'localhost' (10061)"

    I get this message after connecting to database.
    How do i go about solving it?
  • Adarsh Rao
    <?php
    date_default_timezone_set('Europe/copenhagen');

    ?>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="commentstyle.css">
    </head>
    <body>
    <?php
    echo "<form method='POST' action=''>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
    <textarea name='message' ></textarea><br><br>
    <button name='commentSubmit' type='submit'>Comment</button>
    </form>";
    ?>
    </body>
    </html>
    <?php
    $link=mysqli_connect('localhost','root','');
    if(!$link)
    {
    echo' not connected to server' ;
    }
    if(!mysqli_select_db($link,'commentsection'))
    {
    echo'database not selected';
    }
    if(isset($_POST[‘commentSubmit’]))
    {
    $uid =$_POST['uid'];
    $date = $_POST['date'];
    $message = $_POST['message'];
    $sql = "INSERT INTO comments (uid,date,message) VALUES('$uid','$date','$message')";
    if(!mysqli_query($link,$sql))
    {
    echo 'not inserted';
    }
    else
    {
    echo 'comment inserted';
    }
    }
    ?>
    This should help you.😎✌️
  • Alis XBL
    guys this will help you :)

    <?php

    function setComments($conn){
    if(isset($_POST['commentSubmit'])){



    $uid = mysqli_real_escape_string($conn, $_POST['uid']);
    $date = mysqli_real_escape_string($conn, $_POST['date']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    $sql= "INSERT INTO comments(uid, date ,message) VALUES ('$uid', '$date', '$message');";
    mysqli_query($conn,$sql);




    }


    }
  • Ruoethren Pugunisparam
    Hey there,
    I am a fan of your tutorials!! But do you mind doing a class on how to build your own shopping cart, I have seen many tutorials but they don't seem to explain the codes as well as you could!! Much appreciated
  • Andrew Dana
    "Hi there" not showing when I click comment button...there is nothing wrong with my code
  • Abdul Moiz
    i have form which is under echo like this,

    Code :

    <?php

    echo "<form method='POST' action=' ".setComments." ' >

    it giving error undefined index 'f'.

    </form> ";

    ?>

    what is issue?
  • Massimo Valentini
    Hi, very beautiful and useful video!
    I'm using a mac with AMPPS instead of xamp.
    When I write the code $conn= mysqli_connect('localhost', 'root', '', 'commentsection'); it doesn't work.
    I have tried $conn= mysqli_connect('localhost', 'root', 'root', 'commentsection') but it doesn't work as well.
    Do you know what I can do?
  • Anmol goraya
    done every thing but still showing :


    syntax error unexpected '$result' (T_VARIABLE)
  • Divine Iso
    OK I have got it the reason why most peoples data is not being inserted is because of the date we created. I tried it severally yet it doesn't seem to be correct so I deleted the date row from database and also deleted it from my code and very other thing worked perfectly.
    I will post how to insert a date after working on other stuffs..
  • Divine Iso
    Hello please no information from my message field gets inserted into the database table. Please what should I do?
  • Alis XBL
    <?php

    function setComments($conn){

    if(isset($_POST['setComments'])){
    $uid=$_POST['uid'];
    $date=$_POST['date'];
    $message=$_POST['message'];

    $sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
    $result=$conn->query($sql);

    }
    }
    ?>


    what is wrong it does not save data
  • joseph kung'u
    am getting this Parse error: syntax error, unexpected 'include' (T_INCLUDE) in C:\xampp\htdocs\brendan\comments.php on line 3
    i really don't understand and i have followed all the steps
  • Yao Kouassi jean-claude
    Excellent!
  • Daniel Sobolewski
    Can you explain me why you had to put $conn into setComments();? at 17:00
    I dont understand that move.
  • Ankan Bhattacharjee
    had the same error as Tom, below but comments are getting stored in the db.Is this a syntax thing ?? P.S. thanks for the lesson and breaking the tutorial into parts.
    Right onto displaying the data now. :D
  • rogier heeg
    Hello,

    I used your guide to make a comment section a while ago, but only today when i was improving my website i found that when i spam a long 'word' which is longer than our pixel width. i will move out of the commentBox. Is there any way to make it automatically contain itself to the given space?

    Thanks for the guides by the way, great stuff
  • Aiso Milele
    You are an excellent guide, kind sir. I just jumped in on video 43 to figure out how to submit a comment to a database, and your videos got me through it with no trouble at all. My website and I appreciate your hard work. If I had any money to send you, I would send it. Bless you, homie.
  • Ajay Sahu
    You are awesome bro.! Keep up good work. Love from India.
  • faufaufakh
    i still dont understand at 09:25, in this case i use webhosting not xampp, what must i fill in ?
  • faufaufakh
    what diffrent .inc.php and .php ? when u save a file
  • David Something
    As soon as I use the function nothing shows in the page, I am tying to mix things up a bit and combine this with the login system database. So I have a header and the comments are on a different page.
  • David Something
    Is there any reason not to use the same method to write the data to the database as used when doing the register.php in Video 39 I think.
  • Aleksandar Blagojevic
    When I reload page, I am getting this message: "The page isn’t redirecting properly

    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

    This problem can sometimes be caused by disabling or refusing to accept cookies." Same thing with other browsers. What could be the problem?
  • Anh Trung
    It doesnt add the data to the database when i submitted, really dont know why
  • William Sumrall
    I keep getting an error message "Forbidden - You don't have permission to access /comment_system/".setComments($conn)." on this server." when i try to post a message. Can you help me solve this problem?
  • pratik shah
    keep on getting this error can anyone help
    Warning: mysqli_query(): Couldn't fetch mysqli in
  • Tom ford
    I had to put this instead for one of the parts as it didn't seem to add any data in to the database once submitted.....

    $sql = "INSERT INTO comments (uid, date, message) VALUES ('".$uid."', '".$date."', '".$message."')";

    This goes into 'comments.inc.php'

    Great Tutorial by the way its helped a lot
  • Ramon Fernandez
    Later edit: Oh wow. I see now that a few seconds later he changed the function to take the $conn variable as a parameter. If you're following along and get the error I was having, just keep watching.

    Maybe I was getting an error because I did something wrong, but if you get an error about $conn being undefined in your comments.inc.php, the way I fixed it was with:

    global $conn;
    $result = $conn->query($sql);

    it worked after that. If you chose to use the procedural just replace the second line with:
    $result = mysqli_query($conn, $sql);
  • Cameron Dindy
    For some reason when I run reload the page, the function is being called on start instead of when the button is pushed. I don't know where I went wrong.
  • Qudra k
    You're a mirracle :-D. Thank you a lot.
  • kem mrthappy
    execllent video .....
    i got to ask though ..in the place where you placed in the values for the date ...why did you put a double and single colon??
  • James Hoffman
    I'm having trouble connecting to the server. I tried localhost and get the error "access denied for user 'root'@'localhost' (using password yes)" but when instead of localhost i used the IP address from myPHP that says, Web Server: xx.xx.x.xx I get the error "connection failed: Unknown MySQL server host 'xx.xx.x.xx (2)". I am used a shared server from sitegound, so im a little confused on the parameters. Also I dont have the privileges tab in myPHP admin like other people do. please help and thanks.
  • Wesley MacInnis
    Hey, thanks for a series of great tutorials. All of them are very clear and I have greatly improved by knowledge of PHP by watching them. I am building by first website, and tried to transfer your "comment-section" coding verbatim to my new domain and server. When I did this, I received the error :

    "Warning: Cannot modify header information - headers already sent by (output started at . . . commentsection/index.php:17) in . . .commentsection/comments.inc.php on line 82."

    I have done some research on this issue's causes and solutions, but it seems as if the way your comment section was developed necessarily involves co-mingling HTML and PHP.

    Can you offer any advice now to rectify the conflict between "header" commands in comments.inc.php and html / php outputs in index.php?

    Thanks!
  • BiT
    I wonder why video refuses to resize in Chrome. It works nice in Opera though. What can be the issue?