Jump to content

extreme noob, question


Funkysloth

Recommended Posts

kk, i want to make a commenting system for a personal website, just something simple like,

Name:

comment:

and it just displays it on the page. and have it so anyone can post a comment


this is probably really simple, im sorry if this has bin posted or asked before i hanvt looked around the forum alot, i would be super thankfull if anyone could point me in the right direction. thx







Link to comment
Share on other sites

well, this all depends on your preferences

if you want to use MySQL, it's an entirely different approach than say if you wanted to just store the comments in a text file.

I would suggest the latter if you don't have a MySQL server with a user and pass, and at least one database.
Link to comment
Share on other sites

okay, this is what I'll show you.

okay first, if you have phpMyAdmin, go in and execute the following query:

[code]
CREATE TABLE `comments` (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR NOT NULL,
body TEXT NOT NULL,
timestamp VARCHAR NOT NULL
)
[/code]

then, open a text or php editor and start something like this:

[code]
<?php
/*
alright, so now use the mysql_connect() function to make a resource link to your database
arguments are: mysql_connect("server", "username", "password")
also, the @ sign is so that php does not show an error, allowing us to show custom errors if we so desire by using "or die()"
*/
$conn = @mysql_connect("localhost", "username", "password") or die("Connection Error");
/*
now, you need to select a database
you should have already created this db in phpMyAdmin
again use the @ sign to avert long annoying error messages
*/
$rs = @mysql_select_db("database_name_here") or die("DB Selection Error");
/*
now for the MySQL query.
this is the heart of the script that actually selects the data from the database
so you can use it on your comments page.
note that in this case, at the end of the query, the LIMIT can be set to however many comments you want to be displayed at once.  also the ORDER BY `id` DESC makes it so that the newest comments are shown at the top.
*/
$sql = "SELECT * FROM `comments` ORDER BY `id` DESC LIMIT 5";
/*
now that you have everything else you need, execute the query, but in a variable.
note that you're reassigning the mysql_select_db variable, because the db has already been selected
*/
$rs = @mysql_query($sql, $conn) or die(mysql_error());
/*
now for the part your user see's: the data as an array.
since you want a list of these comments, the data will be echoed inside a while loop
*/
while($row = mysql_fetch_array($rs)){
//this is where you can format how each comment is shown on the page
$msg .= "<p>".$row['body']."</p>";
$msg .= "<p><i>Posted by ".$row['name']." on ".$row['timestamp']."</i></p>";
$msg .= "<hr width=\"100%\" />";
}
//after you've looped through the array, echo what results
echo $msg;
//this is basically how you can show a list of row data in mysql through PHP.
?>
[/code]

now that code, coupled with the mysql query i gave you to create the table, should show the data in the table, after you insert some, which I'll show you in my next post

to make the query work, make sure where it says "localhost", "username", "password" inside the connect resource link that you replace that with your host, (usually just localhost, but sometimes different) your username, and your password for your MySQL server.
Link to comment
Share on other sites

Alright so now you know how to display the data, once it's in the table.

but how do you put data into the table?

very simple.

[code]
<?php
//here we'll create the variables based on the post values from the form
  $name = $_POST['name'];
  $body = $_POST['body'];
//now the timestamp will be different; we'll use the date() function to make a string of the date
//that will be entered into the database, all nice and formatted.
  $timestamp = date("F j, Y");
//now do all the connection stuff we did in the last script
  $conn = @mysql_connect("localhost", "username", "password") or die("Connection Error");
  $rs = @mysql_select_db("database_name") or die("DB Selection Error");
//now the sql we write will be different, we'll use an INSERT INTO query
//we'll use the variables from above to insert the data from the form into the db
  $sql = "INSERT INTO `comments` (name, body, timestamp) VALUES(
    '$name',
    '$body',
    '$timestamp'
    )";
/*
now since this form's action is itself, we dont want to execute a query
that has empty values, so we'll make sure the post values are set before we do anything
*/
  if(isset($name) && isset($body)){
    @mysql_query($sql, $conn) or die(mysql_error());
//send user back to see comment they just posted
  header("location:show_comments.php");
}
//set the $self variable to give our form an action
$self = $_SERVER['PHP_SELF'];
//write the form in HTML
?>

<form method="post" action="<?php echo $self; ?>">
Your Name:  <input type="text" name="name" /><br />
Comment<br /> <textarea rows="10" cols="25"></textarea><br />
<input type="submit" value="Post Comment" />
</form>
[/code]

that should get you started.

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.