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

source

38 COMMENTS

  1. I accidentally put the comma inside the apostrophe. You won't get any errors on screen if you make this mistake and no data will be put in the table.
    Was:

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

  2. Thanks for the tutorial, i would like your help! when you have several posts that can be commented on , how do you get the ID of each post such that every comment has an id of the corresponding post where it was made. How do i pass that ID so that i can insert it into the database when am inserting the comment. thanks u.

  3. 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

  4. <?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:laragonwww6comments.inc.php on line 5

    Notice: Undefined index: dat in C:laragonwww6comments.inc.php on line 6

    Can you tell me why?

  5. 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.

  6. <?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.😎✌️

  7. 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);

    }

    }

  8. 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

  9. 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?

  10. 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?

  11. 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..

LEAVE A REPLY

Please enter your comment!
Please enter your name here