Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Guestbook Script, (Using Flat Files, no database)
Pandemonium
post Aug 22 2004, 02:03 AM
Post #1





Guests






Okay, I wrote a cool little Guestbook script a few weeks ago. It is very simple (one page of php) that uses flat files (text files). Using this script, you never need to use a database (you can if you want, but that's up to you). Anyways, here is the php code. Enjoy!

CODE

<?php

print "<form action='whateverthepageis.php' method='post'>";
print "* Your name: " . "<input type='text' name='yourName'>";
print "Email: " . "<input type = 'text' name = 'email'><br>";
print "Website URL: " . "<input type = 'text' name = 'webUrl'>";
print "Website Name: " . "<input type = 'text' name = 'webName'><br>";
print "* Comments: " . "<textarea name='comments'></textarea>";
print "<input type='submit' value='submit'>";
print "</form>";

print "<table>";
if(strlen($yourName) > 4 and strlen($comments) > 10)
{
    $newEmail = "<a href='mailto:" . $email . "'>" . $email . "</a>";
    $newWebUrl = "<a href='". $webUrl."'>". $webUrl . "</a>";

    $fileID = fopen('post.txt', 'a');
    fputs($fileID, "Name: " . $yourName);
    fputs($fileID, "Email: " . $newEmail);
    fputs($fileID, "Website Name: " . $webName);
    fputs($fileID, "Website URL: " . $newWebUrl);
    fputs($fileID, "Comments: " . $comments);
    fclose($fileID);

    $fileID = fopen('post.txt', 'r');
    while(!feof($fileID))
    {
          $gbookLine = fgets($fileID);
          print "<tr><td>" . $gbookLine. "</td></tr>";
    }    
    fclose($fileID);
}
print "</table>";

?>


By the way, if there is an error, I'll correct it.
Go to the top of the page
 
+Quote Post
Spectre
post Aug 22 2004, 03:50 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 873
Joined: 30-July 04
Member No.: 246



Here it is re-written for use with a MySQL database.

Please note that it is a simple, 'on-the-fly' script, so it mightn't be perfect or bugless.

I've also changed one or two things to suit the style in which I usually code. I hope you don't mind, Pandemonium.

CODE
<?php

$sql_db = "guestbook";
$sql_user = "";
$sql_pass = "";
// Make sure you set these variables. The rest can remain unedited,
// assuming you don't want anything else changed.

if(isset($_POST['yourName']) && isset($_POST['comments'])) {
    $name = $_POST['yourName'];
    if(isset($_POST['email'])) { $email = $_POST['email']; }
    if(isset($_POST['webUrl'])) { $url = $_POST['webUrl']; }
    if(isset($_POST['webName'])) { $website = $_POST['webName']; }
    $comments = $_POST['comments'];
    
    mysql_connect("localhost", $sql_user, $sql_pass);
    mysql_select_db($sql_db);
    mysql_query("INSERT INTO guestbook VALUES (0, '$name', '$email', '$url', '$website', '$comments')");
    mysql_close();
}
?>

<p><form action="<?php echo("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']); ?>" method="post">
* Your name: <input type="text" name="yourName">
Email: <input type = "text" name = "email"><br>
Website URL: <input type = "text" name = "webUrl">
Website Name: <input type = "text" name = "webName"><br>
* Comments: <textarea name="comments"></textarea>
<input type="submit" value="submit">
</form></p><p><hr></p><p>

<?php
mysql_connect("localhost", $sql_user, $sql_pass);
mysql_select_db($sql_db);
$result = mysql_query("SELECT * FROM guestbook");
if($result) {
    while($data = mysql_fetch_assoc($result)) {
 echo "<p><b>Name:</b> " . $data['name'] . "<br>\n";
 if(isset($data['email'])) {
     echo "<b>Email:</b> <a href=\"mailto:" . $data['email'] . "\">" . $data['email'] . "</a><br>\n";
 }
 if(isset($data['url'])) {
     echo "<b>Website:</b> <a href=\"" . $data['url'] . "\" target=\"_BLANK\">";
     if(isset($data['website'])) {
   echo $data['website'];
     } else {
   echo $data['url'];
     }
     echo "</a><br>\n";
 }
 echo "<b>Comments:</b><br>\n" . $data['comments'];
 echo "</p>\n<hr>\n";
    }
}
mysql_close();

echo "</p>";

// Script originally created by Pandemonium for use with a flat-file database.
// Re-written for use with MySQL by Spectre.
?>


Here is the table which I used as well (phpMyAdmin dump):

CODE
CREATE TABLE `guestbook` (
 `id` int(5) NOT NULL auto_increment,
 `name` text NOT NULL,
 `email` varchar(30) NOT NULL default '',
 `url` varchar(30) NOT NULL default '',
 `website` text NOT NULL,
 `comments` text NOT NULL,
 PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=4;


This query could be run directly from a PHP script if you want, as long as a connection to MySQL has been established and a database selected.

Keep up the good work, Pandemonium.
Go to the top of the page
 
+Quote Post
Pandemonium
post Aug 22 2004, 04:25 AM
Post #3





Guests






Nice work there Spectre. And no, I don't mind if you changed some of it. smile.gif But thanks for the compliment. I may post some more scripts sometime.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting List Of Directories And Files Using Php(6)
  2. Many Php Script Sites(16)
  3. Script: Php Jukebox(4)
  4. Parse: Error Unexpected T_lnumber(4)
  5. Can You Add Images Into A Mysql Database?(20)
  6. Watermark Your Image With Simple Php Script(35)
  7. Wappychat_oldskool(15)
  8. Free Auction Script(6)
  9. Wappy Buddy V1.10 - Tibia Gold Edition By Wappy & Jon Roig(3)
  10. What Kind Of Script Do You Need ?(15)
  11. Creatting A Playlist Through Php(5)
  12. Very Simple Online Now Script(4)
  13. Html Code Tester. Online Script(15)
  14. Php Downloads Script(4)
  15. Script Help Required: Undefined Variable(3)
  1. Library Script(6)
  2. How Would I Go About Making A Simple "counting" Script?(3)
  3. Forum Script(3)
  4. Php Rediret Script(12)
  5. Download Script For Mp3 Files(0)
  6. How Do I Connect To Live Database With Php Script?(6)
  7. Need Some Help In File Browser(8)
  8. Need Help Installing Dolphin Community Script!(5)
  9. Ms-access Database Question(3)
  10. Guessing Php Script(2)
  11. How To Make A View New Post Script?(5)
  12. Php Guest Online Script(3)
  13. How To Make Php Newsletter Script(3)


 



- Lo-Fi Version Time is now: 13th October 2008 - 01:10 AM