1

I'm trying to create a comment system on my webpage. I want the user to be able in input a comment and have it automatically display on the same page, and reload so that if another user wants to comment the previous comment will also be there. So far, I have created a database that takes in the comments. I have tried to display the comments by querying through my database and printing it out, but it just seems to crash my site.

This is the code I have so far

index.php:

<form action="insert.php" method="GET">
            Comments:
            <input type="text" name="field1_name"/>
            <input type="submit" name="submit" value="submit"/>
</form>

        <?php

            $query="SELECT COMMENTS FROM parentComment";
            $results = mysqli_query($query);

            while ($row = mysqli_fetch_assoc($results)) {

               echo $row['COMMENTS'];

            }
        ?>

insert.php:

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;

$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link,"GRANT ALL ON comment_schema TO 'x'@'localhost'");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if(!empty($_GET["field1_name"])) {
    $field1_name = mysqli_real_escape_string($link, $_GET["field1_name"]);
    // Escape user inputs for security
    $sql = "INSERT INTO parentComment (COMMENTS) VALUES ('$field1_name')";
    $result = mysqli_query($link, $sql);
    // attempt insert query execution
    if ($result) {

        //echo $_GET["field1_name"];

    } else {
       echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
   // close connection
    mysqli_close($link);
}
else{
    die('comment is not set or not containing valid value');
}

So far everything works as in the comments are being inserted into the database. My problem is with retrieving the comments and displaying it to the user on the same page. I've tried to do so, but it seems to be not working. Not sure where I'm going wrong in my implementation (I've implemented it in the index.php file)

Oviya Arasu
  • 193
  • 1
  • 12

1 Answers1

0

You didn't connect to your db for the query:

$results = mysqli_query($query);

Pass the connection to the query:

$results = mysqli_query($link, $query);

It's required.

You also need to make sure that you did establish a connection in that file, otherwise it won't work.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • thank you! i just noticed that as well. also do you know how to make the same html page load so that when I comment, it comments and loads the html page with the comment instead of taking me to another php page? – Oviya Arasu Mar 16 '16 at 01:50
  • @OviyaArasu You're welcome. You can place your entire codes inside the same file, using conditional statements against your GET arrays, and simply change the action to `action=""` which is the equivalent to "self". Then upon successful insert, you can do a header redirect. You need to make sure you're not outputting before header though. Here's a link to consult, a Q&A here http://stackoverflow.com/q/768431/ and the header redirect on PHP.net http://php.net/manual/en/function.header.php - and if you get an error on headers sent http://stackoverflow.com/q/8028957/ – Funk Forty Niner Mar 16 '16 at 01:54