|
|
|
|
![]() ![]() |
| Pandemonium |
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. |
|
|
|
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. |
|
|
|
| Pandemonium |
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.
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 13th October 2008 - 01:10 AM |