You can create a simple light weight database using SQLite3 though it wouldn’t create a text file, but a database file.
First you would need to create the database table.
I called this file createTable.php (I know how original ;D)
[php]<?php
class MyDB extends SQLite3
{
function __construct()
{
/* path to / database name */
$this->open(‘database/guest_book.db’);
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo “Opened database successfully\n”;
}
/* This creates the database table */
$sql =<<<EOF
CREATE TABLE guest_book
(id INTEGER PRIMARY KEY AUTOINCREMENT,
fname VARCHAR(25) NOT NULL,
lname VARCHAR(25) NOT NULL,
email text NOT NULL);
EOF;
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo “Table created successfully\n”;
}
$db->close();
?>[/php]
Then you would create a the guestbook itself by doing something like this:
[php]<?php
try {
/*** Connect to SQLite database ***/
$pdo = new PDO(“sqlite:database/guest_book.db”);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
if ( isset($_POST[‘submit’]) && $_POST[‘submit’] == “Submit” ) {
/* SETUP query for connection to database table /
$query = ‘INSERT INTO guest_book(fname, lname, email) VALUES (:fname, :lname, :email)’;
/ Prepare the query /
$stmt = $pdo->prepare($query);
/ Execute the query */
$result = $stmt->execute(array(’:fname’ => $_POST[‘fname’], ‘:lname’ => $_POST[‘lname’], ‘:email’ => $_POST[‘email’]));
If ($result) {
$message = "Data Successfully Inserted!";
} else {
$message = "Error, something's not right!";
}
}
/* Display Guest Book /
$query = ‘SELECT id, fname, lname, email FROM guest_book ORDER BY id’;
/ Prepare the query /
$stmt = $pdo->prepare($query);
/ Execute the query */
$stmt->execute();
?>
Guest Book
form {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
display: block;
width: 100%;
max-width: 400px;
height: 400px;
background-color: orange;
padding: 10px;
}
fieldset.fieldsetStyle {
border: 1px solid #fff;
height: 360px;
padding: 10px;
}
label.labelStyle, legend.legendStyle {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.0rem;
line-height: 1.5;
color: #fff;
}
legend.legendStyle { padding: 0 10px; }
input.inputStyle {
clear: right;
display: block;
width: 100%;
max-width: 200px;
height: 25px;
font-family: Arial, Helvetica, sans-serif;
font-size: 0.8em;
line-height: 1.0em;
color: #000;
padding: 0px 5px 0px 5px;
margin: 0px;
}
input.formBtn {
border: none;
outline: none;
background-color: #2e2e2e;
color: #fff;
width: 75px;
height: 25px;
margin: 20px 0;
}
p.record {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.2rem;
line-height: 0.8;
}
<?php
/* Create of loop of objects of guest_book table, example $record->fname is First Name in table */
while ($record = $stmt->fetch(PDO::FETCH_OBJ)) {
?>
<?php echo 'Record Number : ' . $record->id . ' Name : ' . $record->fname . ' ' . $record->lname . ' || Email Address : ' . $record->email; ?>
<?php } ?>
<?php echo isset($message) ? $message : 'Guest Book'; ?>
First Name
Last Name
Email Address
<input class="formBtn" type="submit" name="submit" value="Submit">
<input class="formBtn" type="reset" name="clear" value="Clear">
</fieldset>
</form>
[/php]
Now this has no php sanitization or validation and that if you want to password protect you will have to use a full fledged database system (usually MySQL). This probably could be easily converted over using a text file, but since I don’t know text files too well I can’t help you there. I just felt like fooling around SQLite3 tonight and decided your post was a perfect for it. 