Comment box thingy help.

Soldato
Joined
3 Jun 2008
Posts
5,060
Sorry for asking another question but I am having more trouble.
I am trying to add a comment box to my site and have the comments displayed directly underneath.

My code is
table cellpadding=2 cellspacing=4 border=0 bgcolor="#222222">
<form action="index.php" method=post>
<tr><td class=main_txt><B>Your name:</B></td><td><input type=text name=your_name value="" class=form size=20></td></tr>
<tr><td colspan=2 class=main_txt><B>Your comments:</B><BR><textarea name=your_txt cols=37 rows=2 class=form></textarea></td></tr>
<tr><td class=maintxt>Confirmation code:</td><td class=sm><B>394</B> <input type=text name=your_conf value="" class=form size=5> Enter the code exactly as you see it into this box.</td></tr>
<tr><td colspan=2><input type=hidden name=addq value=1><input type=submit name="subm" value="Submit" class=form></td></tr>
</form>
</table>

When I submit a comment is doesnt appear anywhere.

Link to site

Any help greatly appreciated
 
It seems perhaps you are! Do you have PHP/MySQL database usage on your hosting?

You'll need to store comments in a database and then retrieve them for displaying with PHP. Fairly simple, can paste code for you that I've written if that's the case.
 
Ok, well this is my code (which, I forewarn you is probably botched and ugly as hell, but it works!).

On the form you'll want something like;

Code:
<form name="form1" action="index.php?&action2=postmessage" method="post">

..then to put the 'sent' data into the database;

Code:
if($action2 == "postmessage"){
if((!$name) or (!$message)) { // checks if name or message are blank
echo "<p style='color: #cc0000'>You didn't fill in your name or message. Please try again.</p>";
} else {
$today = date ("d/m/y"); // finds and parses todays date
$query = "INSERT INTO messages VALUES ('', '$dbid', '$message', '$name', '$today')";
$result = mysql_query($query, $link); // inserts into db
}
}

..then to display the messages;

Code:
$sql = mysql_query("SELECT * FROM messages WHERE customerid='$dbid' ORDER BY id DESC"); // selects the messages
$rows = mysql_num_rows($sql); // checks the amount of messages

if($rows > 0) { // if there's at least 1 message
echo "<p style='color:#85602E; font-weight:bold; font-size: 14px;'>Current Messages</p><br>";
while($row_customer = mysql_fetch_array($sql)){
$poster = $row_customer['poster'];
$date = $row_customer['date'];
$message = $row_customer['message'];
echo "<b>Posted By</b>: $poster on $date<p style='color:#cccccc;'>$message</p>---<br><br>";
}
} else {
echo "<p style='font-align: center; color:#85602E; font-weight:bold; font-size: 14px;'>No current messages. Be the first!</p>";
}

..and that's pretty much the basics of the coding. You'll then need this to connect to your database (fill in blanks with your own details when given);

Code:
<?
$dbHost = "db host";
$dbUser = "db username";
$dbPass = "db pass";
$dbName = "db name";

$link = @mysql_connect($dbHost, $dbUser, $dbPass);

if(!$link) {
echo "Could not connect to server";
exit;
}
if(!@mysql_select_db($dbName)) {
echo "Could not select database";
exit;
}	
?>

...and you'll need to create a database. Find where to do this on your host, create a MySQL db with the following fields:

Code:
Name: messages
Fields: 5
Field Names: id, customerid, message, poster, date

You can scrap the customer field as I use this for comments split among multiple pages whereas you've only got one.

I know I've rushed through but I don't know how much or little you know so if this is way over your head then shout :).
 
Last edited:
Thanks a lot but I am a little confused.

Could you tell me again what files i need to make and what bit of code to put in them?

cheers.
 
Think I have got all that sorted now but its giving me error messages :(

Comments!!!

PHP Error Message

Warning: mysql_query() [function.mysql-query]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/a2359472/public_html/test/index.php on line 144

Free Web Hosting

PHP Error Message

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/a2359472/public_html/test/index.php on line 144

Free Web Hosting

PHP Error Message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a2359472/public_html/test/index.php on line 145

Free Web Hosting
No current messages. Be the first!

On line 144 i have
$sql = mysql_query("SELECT * FROM messages WHERE customerid='$dbid' ORDER BY id DESC"); // selects the messages

and on line 145 i have
$rows = mysql_num_rows($sql); // checks the amount of messages

link to site

Thanks for any help
 
Last edited:
Still wont work.

Any ideas?

are you sure you're using the correct login settings for that database?

As you've signed up for the free hosting with 000webhost.com all the settings are in the cpanel for your domain (E.g. it explicitly says don't use localhost :) :) ). This is located in the software/services section (click the MySQL option) E.g. mine is in the following format:

Host: mysql2.000webhost.com
DB Name: [your 000webhost login]_[whatever you want]
User: [your 000webhost login]_[whatever you want]
Pass: anything you want




 
are you sure you're using the correct login settings for that database?

As you've signed up for the free hosting with 000webhost.com all the settings are in the cpanel for your domain (E.g. it explicitly says don't use localhost :) :) ). This is located in the software/services section (click the MySQL option) E.g. mine is in the following format:

Host: mysql2.000webhost.com
DB Name: [your 000webhost login]_[whatever you want]
User: [your 000webhost login]_[whatever you want]
Pass: anything you want






Got all them set up correctly.

I fail at php :mad:
 
Post you the code from you php page. Think you've changed something, as i can see the form now :)

I think i understand what you mean now.

This is my code.

<form name="form1" action="index.php?&action2=postmessage" method="post">

<?
if($action2 == "postmessage"){
if((!$name) or (!$message)) { // checks if name or message are blank
echo "<p style='color: #cc0000'>You didn't fill in your name or message. Please try again.</p>";
} else {
$today = date ("d/m/y"); // finds and parses todays date
$query = "INSERT INTO messages VALUES ('', '$dbid', '$message', '$name', '$today')";
$result = mysql_query($query, $link); // inserts into db
}
}

$sql = mysql_query("SELECT * FROM messages WHERE customerid='$dbid' ORDER BY id DESC"); // selects the messages
$rows = mysql_num_rows($sql); // checks the amount of messages<?php


if($rows > 0) { // if there's at least 1 message
echo "<p style='color:#85602E; font-weight:bold; font-size: 14px;'>Current Messages</p><br>";
while($row_customer = mysql_fetch_array($sql)){
$poster = $row_customer['poster'];
$date = $row_customer['date'];
$message = $row_customer['message'];
echo "<b>Posted By</b>: $poster on $date<p style='color:#cccccc;'>$message</p>---<br><br>";
}
} else {
echo "<p style='font-align: center; color:#85602E; font-weight:bold; font-size: 14px;'>No current messages. Be the first!</p>";
}
?>

<?
$dbHost = "mysql2.000webhost.com";
$dbUser = "myusername";
$dbPass = "mypassword";
$dbName = "myusername";

$link = @mysql_connect($dbHost, $dbUser, $dbPass);

if(!$link) {
echo "Could not connect to server";
exit;
}
if(!@mysql_select_db($dbName)) {
echo "Could not select database";
exit;
}
?>
 
You need to have the database connection info before anything else. I usually put it in a separate file, namely db.php, and then include that at the top of every page with a <?include('db.php');?>

Start with that and then we can work out any other problems :)
 
Back
Top Bottom