Jump to content

adding comments to homepage/blog.. sorry, very n00b :)


gwendes

Recommended Posts

I am not the sort of person to post to a forum for assistance! I can usually find all the information I need out there on teh 'net, however, this has me stuck. I have even reached for a bottle of Smirnoff (and some mixers) in an attempt to get to the bottom of what is a simple task.

I am, in general, highly computer literate but at the beginning of an adventure into the world of php/sql.

All I want to do is add a 'comments' script to my homepage so friends can respond to posts that I make, like LiveJournal.

Searching 'comments+PHP+MySQL' throws up tutes for everything but a guide to adding comments to a website.

I even found such a guide, at work, but cannot find it again after 7 hours online today! :(

I have a linux webserver running Apache, MySQL and PHP already, just point me in the right direction and I won't ask any silly questions :)


Link to comment
Share on other sites

use a guestbook tutorial like this:
http://www.php-mysql-tutorial.com/mysql-php-guestbook.php

It's generally the same idea. Just mess around with the code and stuff to make it more for "comments" than a guestbook. you'll probably need to set some kind of "id" for each post in the mysql table that way it knows which post it's commenting to.
Link to comment
Share on other sites

Thanks Jocka, I think I need something that explains the script a little bit more. Or perphaps designed to setup the database using terminal or phpMyadmin specifically.

To clarify, I am very new to all this and I would like to create something and learn at the same time, most sites seem to be trying to teach a-z of the language then the basics.

I think that I'm learning where my strengths lie, I can deal with the hardware and os side, coding is not my forte!

Link to comment
Share on other sites

lol. I know where your coming from. I remember when I first started, it was all jibberish to me.

I'm not sure i'm the right teacher of that. I'd love to help but I'm the worst at explaining things sometimes.

Well .. lets see. Do a SQL query in phpMyAdmin and entire this:
[code]
CREATE TABLE comments (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    p_id INT,
    author VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL
);
[/code]

now some code
[b]comment.php[/b]
<?php

// BE SURE TO PUT YOUR DB INFO IN HERE
$connection = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());

// CONNECT TO DATABASe
$db = mysql_select_db('database', $connection);

// CHECKS IF A COMMENT HAS BEEN SENT
if(!empty($_POST['comment'])){

// CHANGE POSTED INFO INTO STRINGS
$post_id = $_POST['p_id'];
$author = $_POST['author'];
$body = $_POST['body'];
$created = date(); // SET THIS UP AS YOU WANT. SEE www.php.net/date

// ADDING INFO TO DATABASE
// WE ARE MAKING SURE THE COMMENT GOES INTO THE DATABASE
// IF IT DOES IT TAKES YOU BACK TO YOUR POST, IF NOT IT DIES
if($query = mysql_query("INSERT INTO comments (id, p_id, author, body, created) VALUES ('', '$post_id', '$author', '$body', '$created')")){
header ("Location: post.php?id=$post_id"); // ASSUMING POST.PHP IS WHERE THE POSTS ARE
} else {
die('Could not add comment. Please go back and try again');
}
}
?>

Now it's time for your form. Put this at the bottom where the POSTS are where they can comment.

[code]
<form name="comments" action="comment.php" method="POST">
[b]Author:[/b] <input type="text" name="author" value=""><br>
[b]Comment:[/b] <textarea name="comment" rows="10" cols="20"></textarea><br>
<input type="hidden" name="p_id" value=" // YOU NEED YOUR POST ID HERE // ">
<input type="submit" name="submit" value="Submit Comment">
</form>
[/code]

There ya go! That should work.
Link to comment
Share on other sites

Oh I forgot how you view the comments though. Just do something like this where you want it shown:
[code]
$result = mysql_query("SELECT * FROM comments");
while ($row = mysql_fetch_array($result)) {
  echo "Author: " . $row['author'] . "<br>";
  echo "Comment: " . $row['comment'] . "<br>";
  echo "<hr>";
}
[/code]
something like that
Link to comment
Share on other sites

Using jocka's code and working with it will teach you quite a few things.

If instead you decide you want a simple comments system that runs straight out of the box, mine is free from http://www.digitalmidget.com/php_noob2006/comment.php
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.