0

I am following a tutorial series (Link to Video) in which I am learning to create a comments section to a web page. I am using XAMPP as it is what the guy in the video is using. I have finished writing the code which sends the data (name, time, message) to the database and when I go to try it out nothing happens. I checked the database and there is nothing

This is the code:

index.php

<?php

date_default_timezone_set('Europe/London');
include 'dbh.inc.php';
include 'comments.inc.php';
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php
echo"<form method='POST' action='".setComments($conn)."'>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
    <textarea name='message'></textarea> <br>
    <button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>

</body>
</html>

comments.inc.php

<?php

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

        $sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
        $result = mysql_query(- $sql);
    }   
}

dbh.inc.php

<?php

$conn = mysqli_connect('localhost', 'root', '', 'commentsection');

if (!$conn) {
    die("Connection Faild: ".mysql_connect_error());
}

Please help me. thank you

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31

1 Answers1

-2

Change

<?php
echo"<form method='POST' action='".setComments($conn)."'>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
    <textarea name='message'></textarea> <br>
    <button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>

To:

<?php
// action empty send the post data to the this fila again
// setComments function have a condition to work only when POST data is present
setComments($conn);
echo"<form method='POST' action=''>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
    <textarea name='message'></textarea> <br>
    <button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>

And

This:

    $sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
    $result = mysql_query(- $sql);
}   

To:

    // INSERT is the correct sintaxis
    $sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
    $result = mysql_query($sql);
}   

Finally

$conn = mysqli_connect('localhost', 'root', '', 'commentsection');

To:

// mysqli_connect have diferent parameters
$conn = mysql_connect('localhost', 'root', '', 'commentsection');

Tested and working

esdebon
  • 2,460
  • 5
  • 26
  • 33
  • 1
    Tested and working on what? `mysql_query` hasn't been in a current release of PHP for years now. – miken32 Nov 30 '18 at 03:29