Feedback/Commentary System (Beginner/Intermediate)

Josh-Josh- Royal Oak, MI
edited December 2003 in Internet & Media
Feedback/Commentary System
-- written by Josh

Allow your users and guests to leave you feedback/comments on your site through PHP and mySQL.
Guide will include 3 files:
  • config.php
  • viewcomments.php
  • postcomments.php

Config.php
[php]
<?
$dbserver = localhost // server the database is on
$dbadmin = root // username to database
$dbpass = password // password to database
$dbname = userlist // name of database
?>
<?
mysql_pconnect("$dbserver,"$dbadmin","$dbpass");
mysql_select_db("$dbname");

if($submit)
{
$result=MYSQL_QUERY("INSERT INTO $dbname (comment,name)"."VALUES ('NULL','$comment', '$name')");
}
?>
[/php]
This file defines your database aswell as variables/how they are inserted into the database. Just Change info at top ($dbserver, $dbadmin, ect.)


Viewcomments.php
[php]<?
$dbserver = localhost // server the database is on
$dbadmin = root // username to database
$dbpass = password // password to database
$dbname = userlist // name of database
?>
<?
mysql_pconnect("$dbserver,"$dbadmin","$dbpass");
mysql_select_db("$dbname");

$result = mysql_query("select * from $dbname order by id desc");
while($r=mysql_fetch_array($result))
{
$comment=$r["comment"]; //getting each variable from the table
$name=$r["name"]; //getting each variable from the table
?>
<font size="1"><b><? echo $name ?></b> had this to say:
<font size="1"><? echo $comment ?>
<hr width="127" align="left" color="f1f1f1">
<? } ?>
[/php]
Change database variables at top. This connects to the database, and grabs the information that was inputted into the database. Displays the information.

Postcomments.php
[php]
<?
$config = "config.php"; // configuration file
include ("config.php");

echo "<font face='verdana' size='2'>Leave your Feedback</font>$font";
echo "<form action=\"$_SERVER\" method='post'>";
echo "Name: <INPUT TYPE='TEXT' value='' name='name'><br>";
echo "Comment: <INPUT TYPE='TEXT' value='' name='comment'><br>";
echo "<input type="submit" name="submit" value="Send">";
echo "</form>";
?>
[/php]
HTML form, mostly. Lets the user input the data into the database.

Good luck with this. I know I probably screwed something up, and I know someones going to fix me.
-Josh

Comments

  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    Cool little script Josh! :D

    Lots of commenting to help people understand it too :)

    For a beginner coder it points them in the right direction to understanding the fundamentals of PHP :)

    What would I change???

    [php]mysql_pconnect[/php]

    Change that to

    [php]mysql_connect[/php]

    Persistant connections are un-necessary in this or any other script as long as you close the connection at the end of the script. PHP by default will close a SQL connection at the end of a script but it never hurts to make sure anyway ;)

    Error checking, to validate the form and validate the POST variables.

    Oh and change...
    [php]echo "<form action='$php_self' method='post'>";[/php]

    to

    [php]echo "<form action=\"$_SERVER\" method='post'>";[/php]

    $php_self is a variable that will not function with globals = off in PHP.ini. For security reasons, globals should always be off. So use the above :)

    And you should use that
    [php]include ("config.php");[/php]
    on every page and not store database connectivity (especially password :eek: ) in a main script! Security security security!

    But Im not nitpicking because you took the time to write it and it's cool for people to read and learn the fun that is PHP :D

    Keep 'em coming mate :D
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Changes made.

    I am also trying to make these in perspective that people will use them to start coding, keep that in mind, not just to copy and paste them fully. A few errors might allow people to test their skills; improve there knowledge.
Sign In or Register to comment.