Page 1 of 1
Comment / feedback form - now with MySQL!!
Posted: Mon Jun 08, 2009 8:02 pm
by pearjam
Hello again!
Ok, so I'm back with a few questions on setting up a php / mysql

feedback - comment form. Yes, I have mysql now!
Actually, I'm going to need help with the whole thing, most likely... I'm searching info on it and looks like it gets complicated. I'm no where near the 'complicated' level yet. I'm having a hard time even getting out the gate...
1. For instance, it stands to reason that each page would connect to a single, separate db connection script. Then the code in that page would write to it's own table in the db. So when the page comments are displayed, they show comments for that page only (only that table).
Or, would each page connect to it's own database to simplify displaying only comments for that page? Maybe with that page's db connection, table etc, on the same page. That seems like a whole bunch of databases.
Optimization wise, which method is faster / lighter? Maybe there's a whole different way?
2. Here's another thing I'm wondering about. I think using a database will allow me to do things like have a "web site field" on the form, that when entered, turns the name of the user into a link to that site when their comment is displayed. So..., how would you write / separate that info in the table for that page, and reverse by displaying / combining? Is that where rows come in?
See what I mean? I don't have any idea where to start on this thing - I can't find a tutorial that describes, ground up - how to build a feedback form that writes and displays for that page only - over a multi-page site using php / mysql - that's written with a beginner in mind.
Not to mention I have to figure out how to add some kind of captcha, xss type protection. If anyone knows of a tutorial like this, by all means point the way!
Any insight / help is HUGELY appreciated!!! Even a nudge in the right direction would help!
Re: Comment / feedback form - now with MySQL!!
Posted: Mon Jun 08, 2009 8:14 pm
by pearjam
So, on that first question up there - would it be like...
Pages A, B, C, and D point to one separate db connect script.
The php in page A tells it to write to Table A in the single database it's connected to. Page B's php tells it to write to Table B in the same db and so on...
When page A is displayed, the php tells it to display the content of Table A.
(Would that load the single db down with all the requests from all the separate pages?)
Re: Comment / feedback form - now with MySQL!!
Posted: Mon Jun 08, 2009 10:15 pm
by califdon
I don't get a clear picture of what you are trying to do. First off, you certainly wouldn't use multiple databases. When you describe this as a "feedback comment" application, I don't associate that with all these separate pages. Are you really talking about every new user creating a new page for just that user?? That's certainly not what I think of as either feedback or comments, that sounds like a personal web page hosting service or something. Why don't you start by telling us what it is that you want to create. If it's a social networking or blog type application, those are quite sophisticated and I wouldn't recommend that you try that as your first PHP/MySQL project. If it's really feedback (that is, from a user to YOU), I don't see the reasoning behind all the pages. Same with comments. Usually they are just displayed in chronological sequence on one page, probably with pagination when the page fills up.
In any case, you probably only need one table in one database, unless you're thinking of something entirely different. Even if you're going to display them on different pages, that would just be one small field in the table to indicate which page it is associated with.
Re: Comment / feedback form - now with MySQL!!
Posted: Mon Jun 08, 2009 11:06 pm
by pearjam
Ok, let's say you go to the gallery.php page on my site, and you want to make a comment on one of the allbums. You could type the comment in a form and it would display at the bottom of the gallery.php page.
After that, you go to the index page and notice a misspelled word. You could type in the comment box on that page about how let down you feel after seeing such an error and it would show after a refresh at the bottom of the index page.
The comment you made on the gallery page doesn't show on the index page, and vise versa. Because you are giving feed back on that page.
I'm not sure how you got social networking site, but the closest example I can think of regarding the "feed back form" is a wordpress blog. You know how you can read someones post and comment on it, and go to a different page, and the comments change to reflect that page?
That's kind of what I would like to do, only simpler - just a form where visitors can give feed back on the contents of that page.
It's absolutely a personal, amateurishly made learning project, that's non-comercial, has no adds, and I think the only visitors I've had are the guys at work when I ask them to bring my site up in IE because it's a pain trying to code around IE and I use a linux box at work with only ies4linux because our phone system is web based and only works with IE, because the people who made it were geniuses. So I have to run wine. Which is off-topic, but it really bugs me. I also wrote every bit of code on my site by hand in a text editor, and even took all the pictures on my site myself. Plus my girlfriend thinks it's neat, and so do I. Oh I also made the favicon. There.

Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 8:36 am
by pearjam
Any ideas...? Anyone?
Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 12:40 pm
by mikemike
Have a table in your database with the comment_id, time, name of user, comment and most importantly, pagename.
You then just use the same script on every page but reference the pagename in the database.
If you were using some sort of templating system on your website this could be easily implemented right across your website, if you're not then you're going to have to create a file or a function and include it on every page manually (I wouldn't jus copy and paste the code incase you need to make changes at a later stage).
Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 1:05 pm
by mischievous
Hey PearJam, just throwing some nice sites to reference for PHP/MySql
http://www.w3schools.com/PHP/php_mysql_intro.asp
http://www.tizag.com/mysqlTutorial/
XSS (Cross Site Scripting)
http://www.ebrueggeman.com/blog/mysql/p ... injection/
Try and google each problem you run into and figure it out yourself... this forum should be a last resort... reason being... you will learn more by trial and error! But remember were always here if you get stuck. But most of the time everything is a couple searches away to find your answer!
But for table setup I would run with something like:
Table: Comments
Fields: id, page, time, name, comment
Little tip though... when searching through the results the page you pass to the query has to match!
ex: Album Gallery is not the same as AlbumGallery

However, I believe when using a varchar the capitalization of the word doesn't matter.
Query to run:
"SELECT * FROM comments WHERE page = $page ";

Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 3:35 pm
by pearjam
Well, the back story goes like this...
I was trying to create a form, to record comments to a flat file. I was having a problem with one line.
It was strongly suggested here repeatedly across two threads that I switch to mysql. I found that a flat file is locked while writing so I decided to switch to mysql.
So i reformatted my server, and installed mysql and phpmyadmin.
I then looked every where for info on how to layout a database with questions like, one db for several pages - but how to write to one area of the data base and how to display one area of a db on one page, etc...
Everything I found was confusing, so I wrote here and excitedly awaited my response.
My first response was asking me if I was building a social networking site. From the same person in the last two threads that suggested I use mysql, and had seen the 11 lines of code I made to write to the flat file. 11 lines and a flat file wouldn't make a good social networking site...
So now I'm stuck.
I'm going to end up writing it then having everyone tell me "why didn't you write it this way...?"
Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 4:36 pm
by mischievous
MMMMkkay!... this will help ya out not sure how if the code exactly works... I haven't tested it... but for the most part it should work... some others may throw some comments about it... I don't have the time right now to make sure it works... but it should give you the overall idea. There still needs more code added obviously... like add a preview on submit of comment... etc.
Let me know if that works for you... kinda curious
MySql DB Import
Code: Select all
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page_name` varchar(255) NOT NULL,
`author` varchar(255) NOT NULL,
`comment` text NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `comments`
--
INSERT INTO `comments` (`id`, `page_name`, `author`, `comment`, `time`) VALUES
(1, 'album_gallery', 'author1', 'This is author 1''s comment. ', '2009-06-09 15:57:47'),
(2, 'photo_gallery', 'author2', 'This is author 2''s comment. ', '2009-06-09 15:57:47');
Code Rough Draft:
Code: Select all
<?php
class Comments {
function dbConnect($condition){
$DB_ServerName = "localhost";
$DB_User = "root"; //needs to be changed to the user you setup
$DB_Pass = ""; //needs to be changed to the pass for this user
$DB = mysql_connect($DB_ServerName, $DB_User, $DB_Pass);
if($condition == "open")
{
if (!$DB)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("comments", $DB);
}else{
mysql_close($DB);
}
}
function dbInsert($pagename){
$dbConnect('open');
$author = strip_tags($_POST['author']);
$comment = strip_tags($_POST['comment']);
$query = "INSERT INTO comments (page_name, author, comment) VALUES ($pagename, $author, $comment)";
mysql_query($query);
$dbConnect('close');
}
function dbView($pagename){
$dbConnect('open');
$query = "SELECT * FROM comments WHERE page_name = $pagename";
$queryresult = mysql_query($query);
$querycheck = mysql_num_rows($queryresult);
$comments = "";
if ($querycheck > 0)
{
$comments .= "<table id='comments'>";
foreach($queryresult->result() as $comment)
{
$comments .= "<tr><td>".$comment->author."</td><td>".$comment->comment."</td><td>".$comment->time."</td></tr>";
}
$comments .="</table>";
}
return $comments;
$dbConnect('close');
}
}
/*
To make calls to these just simply:
$CM = new Comments();
$CM->dbView('album');
*/
?>
Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 7:46 pm
by pearjam
mikemike - That's a push in the right direction - The foundation makes sense too. Thank you.
mischievous - I checked out those sites before I posted, and others... but may not of been to those exact links - I'll have to check 'em out.
I'll try and dig into that code too and see how much I can break it (jokes)...

Re: Comment / feedback form - now with MySQL!!
Posted: Tue Jun 09, 2009 10:02 pm
by pearjam
I read up on how to create a database, and that helped me figure out how data was written and organized. (1 db, with tables, and fields (rows / columns) in the tables. Now that I know that, this tutorial makes more sense...
http://www.w3schools.com/PHP/php_mysql_connect.asp
It runs through how to create it, connect to it, write to it, and display or pull from it. I'll have questions, I'm sure so I'll keep you guys posted!!
Re: Comment / feedback form - now with MySQL!!
Posted: Fri Jun 12, 2009 11:38 am
by mikemike
It looks a lot more daunting than it really is. It's all quite simple really. You connect to the database, grab the data you want, then display it. I'm sure you won't have too much trouble.