Humble Dude
05-09-2004, 05:27 PM
Hello everyone,
I have a dream. Well, actually, I have a form...& a mySQL db table for it. The dream would be getting the two to work together as I would like them to...& I'm just wondering if someone might be able to give me a hand?
The little setup below actually works. It's basically a tutorial I picked up from somewhere else. I've gone through & done some tweaking to it to make it compatible with my site, of course...& have successfully used it. The results are the data being inserted into the db & a copy of the form's submitted data sent to my email. Which is exactly what I want.
I simply wish to add a couple additional fields to the form...for instance, let's say userage & locale...& achieve the same results with the data inserted & a copy to my email.
I know that I will either need to add rows to mySql's db table - or create a new one...& I don't necessarily have a problem putting the form together. Of course, my difficulties lie in the php/mySQL instructions (the *feedback* portion of this post).
I'm just beginning to really *get* PHP...just beginning. I learned & successfully implemented php includes yesterday...WOOOOO HOOOOOOO!! Which was a major ordeal for me. When I finally realized how easy it was (except for the .htaccess part) I about fell over.
I am very, very, very green with mySQL...although I have run through a couple of tutorials...obviously.
Anyway - my apologies for the length of this post, but better too much info than not enough, right??
Here are the steps I have gone through in setting up this *form/shoutbox*...& the process my form goes through:
CREATE mySQL TABLE:
CREATE TABLE feedback (
name varchar(50),
date varchar(40),
email varchar(50),
comment longtext,
id int(4) DEFAULT '0' NOT NULL auto_increment,
PRIMARY KEY (id)
);
CONFIG.INC FILE:
<?php
$PostPage = "post.php"; //Where people will sign the guestbook
$gbPage = "view.php"; //Where people will view the guestbook
$allowHTML = 0; //Change this if you want people to post HTML in posts 1 = Yes, 0 = No
$notify = 0; //When this is set, you will be emailed every time your guestbook is signed 1 = yes, 0 = No
$my_email = "youremail@emailhost.com"; //Enter your email address here
$subject = "New Guestbook Entry" ; //This is the subject of the notification email
$username = "myname_mydb"; //Database Username
$password = "mypassword"; //Database password
$db_name = "username_db"; //Database name
$limit = 20; //This is the number of entries that will be displayed per page
?>
POST:
<?
// only change this include if you renamed the file
include ("config.inc.php");
echo $error1; ?>
<FORM ACTION="feedback.php" METHOD=POST>
<P> Name:<BR>
<INPUT TYPE=text NAME=name SIZE=30 maxlength=80>
<BR>
Email (Not Required):<BR>
<INPUT TYPE=text NAME=email SIZE=30 maxlength=80>
<br>
Comments: <BR>
<textarea name="comments" cols="30" rows="5" wrap="VIRTUAL"></textarea>
<BR>
</P>
<P><INPUT TYPE=submit NAME=gb VALUE="Add Message">
Please press only once: this may take a moment.
</FORM>
FEEDBACK:
<?php
// only change this include if you renamed the file
include ("config.inc.php");
// lets make sure they fill out the form before they submit
if (($name == "") AND ($email == "") AND ($comments == "")):
// This is the error message displayed...change this if you want...
$error1 = "<font face='Arial' color='Red'><STRONG>Please fill out the form:</STRONG></font><br><br>";
include($PostPage);
else:
if ($allowHTML == 0):
$name = ereg_replace("<","<",$name);
$name = ereg_replace(">",">",$name);
$email = ereg_replace("<","<",$email);
$email = ereg_replace(">",">",$email);
$url = ereg_replace("<","<",$url);
$url = ereg_replace(">",">",$url);
$urltitle = ereg_replace("<","<",$urltitle);
$urltitle = ereg_replace(">",">",$urltitle);
$referral = ereg_replace("<","<",$referral);
$referral = ereg_replace(">",">",$referral);
$comments = ereg_replace("<","<",$comments);
$comments = ereg_replace(">",">",$comments);
endif;
//connect to the database
// change localhost to your db host name
$mysql_link = mysql_pconnect( "localhost", "$username", "$password") // Enter your database info here
or die( "Unable to connect to server");
mysql_select_db( "$db_name") or die( "Unable to select database");
$insert = "INSERT INTO feedback(name, date, email, comment) VALUES('$name', SYSDATE(), '$email', '$comments')";
$mysql_insert = mysql_query($insert, $mysql_link)
or die("please notify $my_email that the script is not inserting entries<BR><BR>$name,<br>$email,<br>$comments<br><br>$insert");
// Email the new post
if ($notify == 1):
$comments = ereg_replace("<BR>","\n",$comments);
mail
(
"$my_email",
"guestbook entry",
"Name : $name
Email : $email
Note:
$comments\n
",
"From: $email\n"
);
endif;
//check for double entries
$name_chk = $name;
$email_chk = $email;
$comments_chk = $comments;
include($gbPage);
endif;
?>
VIEW:
<?php
// only change this include if you renamed the file
include ("config.inc.php");
//connect
// change localhost to your db host name
mysql_pconnect( "localhost", "$username", "$password")
or die( "Unable to connect to SQL server");
mysql_select_db( "$db_name") or die( "Unable to select database");
$numresults=mysql_query("SELECT name, date, email, comment FROM feedback");
$numrows=mysql_num_rows($numresults)
or die ("query 1 failed");
if (empty($offset)) {
$offset=0;
}
$query = "SELECT name, date, email, comment FROM feedback order by date DESC limit $offset,$limit";
$result = mysql_query ($query)
or die ("query 2 failed");
while ($row = mysql_fetch_row ($result))
{
for ($i = 0; $i < mysql_num_fields ($result); $i++)
{
if ($i > 0)
print ("<br>");
if ($i == 0){
print "<b>Name: </b>";
}
else if ($i == 1){
print "<b>Date: </b>";
}
else if ($i == 2){
print "<b>Email: </b>";
}
else{
print "<b>Comment:</b><br>";
}
print ($row[$i]);
}
print "<br><br><center><hr></center>";
print ("<P>");
}
if ($offset >= 3) {
$prevoffset = $offset - $limit;
print "<a href=\"view.php?offset=$prevoffset\">PREV</a> \n";
}
$pages=intval($numrows/$limit);
if ($pages < ($numrows/$limit)){
$pages=($pages + 1);
}
for ($i = 1; $i <= $pages; $i++) {
$newoffset = $limit*($i-1);
if ($newoffset == $offset) {
print "$i \n";
} else {
print "<a href=\"view.php?offset=$newoffset\">$i</a> \n";
}
}
//show next if not last
if (! ( ($offset/$limit) == ($pages - 1) ) && ($pages != 1) ) {
$newoffset = $offset+$limit;
print "<a href=\"view.php?offset=$newoffset\">NEXT</a><p>\n";
}
?>
Thanks much for any help, ideas, or suggestions. They are certainly greatly, greatly appreciated.
Best regards to all,
Humble Dude
P.S. - Is there a PHP instruction that will open a particular link within my site in a new browser window? Similar to the "Open New Browser" client-side in Dreamweaver MX?
Thank you...
I have a dream. Well, actually, I have a form...& a mySQL db table for it. The dream would be getting the two to work together as I would like them to...& I'm just wondering if someone might be able to give me a hand?
The little setup below actually works. It's basically a tutorial I picked up from somewhere else. I've gone through & done some tweaking to it to make it compatible with my site, of course...& have successfully used it. The results are the data being inserted into the db & a copy of the form's submitted data sent to my email. Which is exactly what I want.
I simply wish to add a couple additional fields to the form...for instance, let's say userage & locale...& achieve the same results with the data inserted & a copy to my email.
I know that I will either need to add rows to mySql's db table - or create a new one...& I don't necessarily have a problem putting the form together. Of course, my difficulties lie in the php/mySQL instructions (the *feedback* portion of this post).
I'm just beginning to really *get* PHP...just beginning. I learned & successfully implemented php includes yesterday...WOOOOO HOOOOOOO!! Which was a major ordeal for me. When I finally realized how easy it was (except for the .htaccess part) I about fell over.
I am very, very, very green with mySQL...although I have run through a couple of tutorials...obviously.
Anyway - my apologies for the length of this post, but better too much info than not enough, right??
Here are the steps I have gone through in setting up this *form/shoutbox*...& the process my form goes through:
CREATE mySQL TABLE:
CREATE TABLE feedback (
name varchar(50),
date varchar(40),
email varchar(50),
comment longtext,
id int(4) DEFAULT '0' NOT NULL auto_increment,
PRIMARY KEY (id)
);
CONFIG.INC FILE:
<?php
$PostPage = "post.php"; //Where people will sign the guestbook
$gbPage = "view.php"; //Where people will view the guestbook
$allowHTML = 0; //Change this if you want people to post HTML in posts 1 = Yes, 0 = No
$notify = 0; //When this is set, you will be emailed every time your guestbook is signed 1 = yes, 0 = No
$my_email = "youremail@emailhost.com"; //Enter your email address here
$subject = "New Guestbook Entry" ; //This is the subject of the notification email
$username = "myname_mydb"; //Database Username
$password = "mypassword"; //Database password
$db_name = "username_db"; //Database name
$limit = 20; //This is the number of entries that will be displayed per page
?>
POST:
<?
// only change this include if you renamed the file
include ("config.inc.php");
echo $error1; ?>
<FORM ACTION="feedback.php" METHOD=POST>
<P> Name:<BR>
<INPUT TYPE=text NAME=name SIZE=30 maxlength=80>
<BR>
Email (Not Required):<BR>
<INPUT TYPE=text NAME=email SIZE=30 maxlength=80>
<br>
Comments: <BR>
<textarea name="comments" cols="30" rows="5" wrap="VIRTUAL"></textarea>
<BR>
</P>
<P><INPUT TYPE=submit NAME=gb VALUE="Add Message">
Please press only once: this may take a moment.
</FORM>
FEEDBACK:
<?php
// only change this include if you renamed the file
include ("config.inc.php");
// lets make sure they fill out the form before they submit
if (($name == "") AND ($email == "") AND ($comments == "")):
// This is the error message displayed...change this if you want...
$error1 = "<font face='Arial' color='Red'><STRONG>Please fill out the form:</STRONG></font><br><br>";
include($PostPage);
else:
if ($allowHTML == 0):
$name = ereg_replace("<","<",$name);
$name = ereg_replace(">",">",$name);
$email = ereg_replace("<","<",$email);
$email = ereg_replace(">",">",$email);
$url = ereg_replace("<","<",$url);
$url = ereg_replace(">",">",$url);
$urltitle = ereg_replace("<","<",$urltitle);
$urltitle = ereg_replace(">",">",$urltitle);
$referral = ereg_replace("<","<",$referral);
$referral = ereg_replace(">",">",$referral);
$comments = ereg_replace("<","<",$comments);
$comments = ereg_replace(">",">",$comments);
endif;
//connect to the database
// change localhost to your db host name
$mysql_link = mysql_pconnect( "localhost", "$username", "$password") // Enter your database info here
or die( "Unable to connect to server");
mysql_select_db( "$db_name") or die( "Unable to select database");
$insert = "INSERT INTO feedback(name, date, email, comment) VALUES('$name', SYSDATE(), '$email', '$comments')";
$mysql_insert = mysql_query($insert, $mysql_link)
or die("please notify $my_email that the script is not inserting entries<BR><BR>$name,<br>$email,<br>$comments<br><br>$insert");
// Email the new post
if ($notify == 1):
$comments = ereg_replace("<BR>","\n",$comments);
(
"$my_email",
"guestbook entry",
"Name : $name
Email : $email
Note:
$comments\n
",
"From: $email\n"
);
endif;
//check for double entries
$name_chk = $name;
$email_chk = $email;
$comments_chk = $comments;
include($gbPage);
endif;
?>
VIEW:
<?php
// only change this include if you renamed the file
include ("config.inc.php");
//connect
// change localhost to your db host name
mysql_pconnect( "localhost", "$username", "$password")
or die( "Unable to connect to SQL server");
mysql_select_db( "$db_name") or die( "Unable to select database");
$numresults=mysql_query("SELECT name, date, email, comment FROM feedback");
$numrows=mysql_num_rows($numresults)
or die ("query 1 failed");
if (empty($offset)) {
$offset=0;
}
$query = "SELECT name, date, email, comment FROM feedback order by date DESC limit $offset,$limit";
$result = mysql_query ($query)
or die ("query 2 failed");
while ($row = mysql_fetch_row ($result))
{
for ($i = 0; $i < mysql_num_fields ($result); $i++)
{
if ($i > 0)
print ("<br>");
if ($i == 0){
print "<b>Name: </b>";
}
else if ($i == 1){
print "<b>Date: </b>";
}
else if ($i == 2){
print "<b>Email: </b>";
}
else{
print "<b>Comment:</b><br>";
}
print ($row[$i]);
}
print "<br><br><center><hr></center>";
print ("<P>");
}
if ($offset >= 3) {
$prevoffset = $offset - $limit;
print "<a href=\"view.php?offset=$prevoffset\">PREV</a> \n";
}
$pages=intval($numrows/$limit);
if ($pages < ($numrows/$limit)){
$pages=($pages + 1);
}
for ($i = 1; $i <= $pages; $i++) {
$newoffset = $limit*($i-1);
if ($newoffset == $offset) {
print "$i \n";
} else {
print "<a href=\"view.php?offset=$newoffset\">$i</a> \n";
}
}
//show next if not last
if (! ( ($offset/$limit) == ($pages - 1) ) && ($pages != 1) ) {
$newoffset = $offset+$limit;
print "<a href=\"view.php?offset=$newoffset\">NEXT</a><p>\n";
}
?>
Thanks much for any help, ideas, or suggestions. They are certainly greatly, greatly appreciated.
Best regards to all,
Humble Dude
P.S. - Is there a PHP instruction that will open a particular link within my site in a new browser window? Similar to the "Open New Browser" client-side in Dreamweaver MX?
Thank you...