I have just uploaded a working example of this put to use, at "hippi.trap17.com/comments/comments.php"
This is my second tutorial on PHP. It's not as long as the last one, and I'm going to try and explain this one better to make sure you know exactly what's going on.
I just figured this out today, so if it's missing any functionality, or there is anything wrong with it, let me know. It worked for me.
This comment page isn't supposed to look good, it's just supposed to work, but you can change the HTML however you want to make it look better and change the overall layout.
First of all, you need to make your config.php in the same directory as your comments page.
CODE
<?
$dbserver="localhost";
$user="root";
$passwd=""; //Enter your password here
$connect=mysql_connect($dbserver,$user, $passwd);
$db = 'forum'; //just a database I picked, use one you already have, or make another one if you really want to.
$pre = 'com_'; //use any prefix you want, as long as nothing else could have it.(you don't really need this, it's just if you have
// something else with a table named 'comments.')
?>
$dbserver="localhost";
$user="root";
$passwd=""; //Enter your password here
$connect=mysql_connect($dbserver,$user, $passwd);
$db = 'forum'; //just a database I picked, use one you already have, or make another one if you really want to.
$pre = 'com_'; //use any prefix you want, as long as nothing else could have it.(you don't really need this, it's just if you have
// something else with a table named 'comments.')
?>
You might want to make it echo something, using "echo 'something';", after the connection to make sure it connected. You could also use the "or die('something')" on the line where it connects, but I've never really gotten that to work.
Now, you need to have your comments.php. The HTML for it should have a form with the method as POST and a text input, a textArea, and a submit button. Make sure you name them 'name', and 'comment'. If you name them anything else, you'll have to change all of your code later to get it to work, so it's just easier to leave the names alone.
CODE
<form method='post'><h3>Name:</h3><br><input type='text' name='name'><br>
<h3>Comment:</h3>
<textarea name='comment'rows="10" cols="50" wrap='hard'></textarea>
<input type='submit' value='submit'></form>
<h3>Comment:</h3>
<textarea name='comment'rows="10" cols="50" wrap='hard'></textarea>
<input type='submit' value='submit'></form>
I like "wrap='hard'" better than 'soft' and 'off'. It depends on what you want to do. There are HTML tutorials elsewhere that you could look at to see the difference.
Now, you need to start making your functions.
The first one you need should be a submitter function.
CODE
function submitComment($db, $pre, $name, $com, $connect) { //database, prefix, name, comment, connection
mysql_select_db($db);
$sql = 'CREATE TABLE IF NOT EXISTS ' . $pre . 'comments (id int auto_increment, name text, comment text, primary Key(id));';
mysql_query($sql, $connect); //Makes the table if it doesn't exist. The "." is the concatenation character for PHP
$sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");';
mysql_query($sql, $connect); //inserts your comment into the table.
}
mysql_select_db($db);
$sql = 'CREATE TABLE IF NOT EXISTS ' . $pre . 'comments (id int auto_increment, name text, comment text, primary Key(id));';
mysql_query($sql, $connect); //Makes the table if it doesn't exist. The "." is the concatenation character for PHP
$sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");';
mysql_query($sql, $connect); //inserts your comment into the table.
}
The "exiter" function is something I made up to make it so there are no errors when you use quotes, apostrophes, or backslashes. I'm not sure if there are any other characters you would need it for, though. You will use it when you call the submitComment function later.
CODE
function exiter($str) { //makes it so you can insert quotes
for($i=0;$i<strlen($str);$i++) { //loops through every character
$sub = substr($str, $i, 1); // character at the $i position
if($sub=='"' || $sub=="'" || $sub==chr(92)) { //if it has a " or ' or \, it will add a \ before it so it won't think of it as that character.
$beg = substr($str, 0, $i);
$end = substr($str, $i+1, strlen($str));
$str = $beg . chr(92) . $sub . $end; //chr(92) is the backslash
$i++;
}
}
return $str;
}
for($i=0;$i<strlen($str);$i++) { //loops through every character
$sub = substr($str, $i, 1); // character at the $i position
if($sub=='"' || $sub=="'" || $sub==chr(92)) { //if it has a " or ' or \, it will add a \ before it so it won't think of it as that character.
$beg = substr($str, 0, $i);
$end = substr($str, $i+1, strlen($str));
$str = $beg . chr(92) . $sub . $end; //chr(92) is the backslash
$i++;
}
}
return $str;
}
Now that you have that, you can call your submitter function.
CODE
if ($_POST['name'] && $_POST['comment']) submitComment($db, $pre, $_POST['name'], exiter($_POST['comment']), $connect); //if there is a name and a comment, it will put them in the database.
Now, you need to display your comments from your table. The my showComments function does the trick. Later, though, you might want to change the appearance of the table. If you know anything about HTML, you should be able to do this easily.
CODE
function showComments($db, $pre, $connect) {
mysql_select_db($db);
$numcols = 3; //id, name, comment
$colnames=array(0=>'id',1=>'name',2=>'comment'); //creates an array to use later to read from the database.
$sql = "select * from " . $pre . "comments order by id desc"; //Makes it so it shows the comments in reverse order, so it shows
$result = mysql_query($sql, $connect); //the most recent at the top.
$numrows = mysql_numrows($result);
echo "<table>"; //starts the table, change it's properties however you want.
for ($y=0;$y<$numrows;$y++) { //loops through every row
echo "<tr>";
for ($x=0;$x<$numcols;$x++) { //loops through every column
if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>"; //The "if" makes it so it doesn't show the row's ID
}
echo "</tr>";
}
echo "</table>";
}
mysql_select_db($db);
$numcols = 3; //id, name, comment
$colnames=array(0=>'id',1=>'name',2=>'comment'); //creates an array to use later to read from the database.
$sql = "select * from " . $pre . "comments order by id desc"; //Makes it so it shows the comments in reverse order, so it shows
$result = mysql_query($sql, $connect); //the most recent at the top.
$numrows = mysql_numrows($result);
echo "<table>"; //starts the table, change it's properties however you want.
for ($y=0;$y<$numrows;$y++) { //loops through every row
echo "<tr>";
for ($x=0;$x<$numcols;$x++) { //loops through every column
if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>"; //The "if" makes it so it doesn't show the row's ID
}
echo "</tr>";
}
echo "</table>";
}
Don't worry, if you don't already have the table, it won't show anything. If it does for you, let me know.
Now, you only need to call the function.
CODE
showComments($db, $pre, $connect);
You don't need an if statement for this, because it should show the previous comments no matter what.
Here is the entire code: (not counting config.php)
CODE
<form method='post'><h3>Name:</h3><br><input type='text' name='name'><br>
<h3>Comment:</h3>
<textarea name='comment'rows="10" cols="50" wrap='hard'></textarea>
<input type='submit' value='submit'></form>
<?
require_once('config.php');
function exiter($str) { //makes it so you can insert quotes
for($i=0;$i<strlen($str);$i++) {
$sub = substr($str, $i, 1);
if($sub=='"' || $sub=="'" || $sub==chr(92)) {
$beg = substr($str, 0, $i);
$end = substr($str, $i+1, strlen($str));
$str = $beg . chr(92) . $sub . $end; //chr(92) is the backslash
$i++;
}
}
return $str;
}
function submitComment($db, $pre, $name, $com, $connect) {
mysql_select_db($db);
$sql = 'CREATE TABLE IF NOT EXISTS ' . $pre . 'comments (id int auto_increment, name text, comment text, primary Key(id));';
mysql_query($sql, $connect);
$sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");';
mysql_query($sql, $connect);
}
function showComments($db, $pre, $connect) {
mysql_select_db($db);
$numcols = 3; //id, name, comment
$colnames=array(0=>'id',1=>'name',2=>'comment');
$sql = "select * from " . $pre . "comments order by id desc";
$result = mysql_query($sql, $connect);
$numrows = mysql_numrows($result);
echo "<table>";
for ($y=0;$y<$numrows;$y++) {
echo "<tr>";
for ($x=0;$x<$numcols;$x++) {
if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
if ($_POST['name'] && $_POST['comment']) submitComment($db, $pre, $_POST['name'], exiter($_POST['comment']), $connect);
showComments($db, $pre, $connect);
//Whenever you want to delete the comments, just do a drop table on your PHPmyAdmin.
//I'll probably figure out how to delete them depending on when they were submitted.
?>
<h3>Comment:</h3>
<textarea name='comment'rows="10" cols="50" wrap='hard'></textarea>
<input type='submit' value='submit'></form>
<?
require_once('config.php');
function exiter($str) { //makes it so you can insert quotes
for($i=0;$i<strlen($str);$i++) {
$sub = substr($str, $i, 1);
if($sub=='"' || $sub=="'" || $sub==chr(92)) {
$beg = substr($str, 0, $i);
$end = substr($str, $i+1, strlen($str));
$str = $beg . chr(92) . $sub . $end; //chr(92) is the backslash
$i++;
}
}
return $str;
}
function submitComment($db, $pre, $name, $com, $connect) {
mysql_select_db($db);
$sql = 'CREATE TABLE IF NOT EXISTS ' . $pre . 'comments (id int auto_increment, name text, comment text, primary Key(id));';
mysql_query($sql, $connect);
$sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");';
mysql_query($sql, $connect);
}
function showComments($db, $pre, $connect) {
mysql_select_db($db);
$numcols = 3; //id, name, comment
$colnames=array(0=>'id',1=>'name',2=>'comment');
$sql = "select * from " . $pre . "comments order by id desc";
$result = mysql_query($sql, $connect);
$numrows = mysql_numrows($result);
echo "<table>";
for ($y=0;$y<$numrows;$y++) {
echo "<tr>";
for ($x=0;$x<$numcols;$x++) {
if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
if ($_POST['name'] && $_POST['comment']) submitComment($db, $pre, $_POST['name'], exiter($_POST['comment']), $connect);
showComments($db, $pre, $connect);
//Whenever you want to delete the comments, just do a drop table on your PHPmyAdmin.
//I'll probably figure out how to delete them depending on when they were submitted.
?>
If this script doesn't work, you might need to change the first "<?" to "<?php". I've configured my PHP to work either way.
If you have any suggestions, or know a way to make it delete old comments, please let me know.
Don't read past this if you don't care how I learned this(it's kind of boring, and I blabber(<--funny word
I'm new to PHP, HTML, and CSS, so if anyone knows of any good tutorials, please help.
It took me around two hours to get this to work because I didn't know exactly what I was doing. I learned most of it as I made the page, like I just found out what an exitor was, so it took like 20 minutes to figure out why it wouldn't insert anything with quotes or anything; so when I found that out, I made up a function to get it to work anyway. Trying to get my "exiter" function to work is when I learned about PHP string handling.
I also tried to make a version of this that works with text files, so I learned a lot about file handling, too; but I found out that it wasn't efficient enough that way, and you can end up with a lot of errors if there is more than one person at a time, and it's really slow.
Sorry if I'm starting to annoy you. I'll just shut up.


