help me with php

I am coding a guestbook using only php and html I have to input entries via a form asking for the users name and email address, I cant figure out what is needed to allow users to enter in information and also still eliminating any duplicate names and/or emails
this is my code so far

    <head>
            <title>guestbook entries to visitors.txt</title>
    </head>
<?php $Fname=$_POST["gFName"]; $Lname=$_POST["gLName"]; $email=$_POST["guestMail"]; $GuestRegister = fopen("visitors.txt", "a"); if($Fname=="" && $Lname=="" && $email=="") { echo"You didn't fill anything out"; }elseif($Fname=="" || $Lname=="") { echo"You need to fill out your name"; }elseif($Fname=="" && $Lname=="") { echo"you need to fill out your name"; }elseif($email=="") { echo"you need to fill out your email address"; } elseif($Lname=="" || $email=="") { echo"you need to fill out all required information"; }elseif($Fname=="" || $email=="") { echo"you need to fill out all required information"; } else { $list = file('visitors.txt'); // Convert every line to lowercase $list = array_map("strtolower", $list); // Remove duplicates $list =array_unique($list); // Write back to file file_put_contents('visitors.txt', implode('', $list)); $NewGuest = "$Fname $Lname~$email
\n"; fwrite($GuestRegister, $NewGuest); fclose($GuestRegister); readfile("visitors.txt"); } ?>

I notice a lot of posts of this going around here lately and I still think it’s crazy that the instructor doesn’t want MySQL to be used. :slight_smile:

Anyways, here is a fully working solution to the assignment and I know it’s far from perfect; however, it works. :wink:

The php file (You can call it anything you like):
[php]<?php
/* Get the path and filename that you are currently on. /
$phpSelf = filter_input(INPUT_SERVER, ‘PHP_SELF’, FILTER_SANITIZE_URL);
/
A simple text file /
$file = “guest_book.txt”;
/
Variables and Arrays */
$record = array();
$data = array();
$message = NULL;

function getRecords($file = “guest_book.txt” ) {
/* Setup up Display */
if (file_exists($file)) {

	/* Grab each line of text file in an array */
	$list = file($file);
	
	/* Explode each line string in the array into an inner array */
	foreach ($list as $key => $value) {
		$output[$key] = explode(",", $value); 
	}
	
	/* Convert to associative inner array */	
	for ( $x = 0; $x < count($output); $x++) {
		$record[$x]['fname'] = $output[$x][0];
		$record[$x]['lname'] = $output[$x][1];
		$record[$x]['email'] = $output[$x][2];
	}
	unset($list);
	unset($output);
	return $record;
}	

}

$record = getRecords(); // Retrieve Guest Book Records:

/* Validate User’s Input */
function validateInput(array $posts) {
foreach ($posts as $key => $value) {
if ( $value == NULL ) {
return “All fields must be entered!”;
}
}
return NULL;
}

/* Function to check for Duplicate Records /
function check_for_duplicates(array $data) {
/
Assemble Full Name */
$fullName = $data[‘fname’] . ’ ’ . $data[‘lname’];

$record = getRecords(); // Makes sure it's the current database:
/* Search through Array */
for ($x=0; $x < count($record); $x++) {
	$blog = (object) $record[$x]; // I like working objects instead of associate arrays:
	/* Check user's input against database */
	if ( strtolower($fullName) == strtolower($blog->fname . ' ' . $blog->lname) ) {
		return 'Name already in database'; // Return if there's a match:
	}
	
} // End of For Loop 
return NULL;

} // End of Duplicate Records Function:

/* Save to the text file */
if ( isset($_POST[‘submit’]) && $_POST[‘submit’] == “Submit” ) {

$data['fname'] = trim($_POST['fname']);
$data['lname'] = trim($_POST['lname']);
$data['email'] = $_POST['email'];
				
/* validate user's input */
$message = validateInput($_POST);
/* No need to check for duplicates if it fails validation */
if (!$message) {
	/* Function to check to see if there are duplicate records */
	$message = check_for_duplicates($data);
}

/* Insert Record into Database if there are no errors */
if ( empty($message) ) {
	/* Convert to a string, insert , and \n into the string */
	$data = $data['fname'] . ',' . $data['lname'] . ',' . $data['email'] . "\n";	
	/* Save to text file in directory database with file name guest_book.txt */
	$result = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
	/* Check Results of insert */
	if ($result) {
		$message =  $result . ' bytes written to file. ';
		unset($record);
		$record = getRecords();
		/* Clear the Input Value Attributes */
		unset($firstName);
		unset($lastName);
		unset($email);
	} else {
		die('There was an error writing to this file');
	}
}

} // End of If Statement:
?>

Guest Book
<?php echo isset($message) ? $message : 'Guest Book'; ?> First Name Last Name Email Address
[/php]

and here is the css file:

body { font-size: 100%; padding: 0; margin: 0; } .guestbook { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; display: block; width: 100%; max-width: 600px; height: auto; padding: 10px; margin: 0 auto; } form { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; display: block; width: inherit; height: auto; background-color: orange; padding: 10px; } fieldset.fieldsetStyle { border: 1px solid #fff; 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; } /* Guest Book Display */ ul { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; border: 1px solid #2e2e2e; display: block; width: 100%; height: 200px; overflow-y: scroll; overflow-x: hidden; padding: 10px 20px; } li { list-style: none; text-align: left; } a.record { font-family: Arial, Helvetica, sans-serif; font-size: 1.0rem; line-height: 1.5; text-decoration: none; color: #2e2e2e; } a.record:hover { color: blue; }

My suggestion is still try to get yours to work, but use this as a guide or template if you get stuck. HTH 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service