PokeZam Forums  

Go Back   PokeZam Forums > Off-Topic > Technology > Tutorials

Tutorials Here are technology tutorials. Feel free to post your own, but they may be taken down.

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 03-28-2008
Renn's Avatar
Renn Renn is offline
~Renamon
 
Join Date: Feb 2008
Location: Kyoto, Japan
Age: 14
Posts: 35
Default PHP Tutorial: Scripting Your Website

Tutorial:.

In this Tutorial, I will explain you about managing your news, by discussing about Content Management Systems, mostly known as CMS. And A Simple Guestbook.

Requirements
A PHP Website (obviously)
A MySQL Database
Little Bit knowledge of PHP

To learn PHP, please go to: http://killerphp.com

Making A Guestbook
The first thing we are going to need to do is determine what data we want to store, for a guestbook we will most likely want to store the users name, email address, the date and their comments.


To start, create a database called 'guestbook' (always keep database names relevant) and then insert this query:
Code:
CREATE TABLE guestbook ( 
ID int NOT NULL AUTO_INCREMENT, 
name varchar(30), 
email varchar(60), 
dateposted date, 
comment text, 
INDEX (ID) 
);
Here is the query, we are creating a table called guestbook with 5 fields.

The next thing we are going to need to create is the form in which users will input their data.

Create a page called, send.php
Code:
<form name="signguestbook" method="POST" action="add.php"> 
<table cellspacing="4" style="padding: 0px; width: 500px; 
border: 0px;"> 
<tr> 
<td align="right"><b>Name:</b></td><td><input type="text" name="name" /></td> 
</tr> 

<tr> 
<td align="right"><b>Email:</b></td><td><input type="text" name="email" /></td> 
</tr> 

<tr> 
<td colspan="2" align="center"><b>Comment:</b><br /> 
<textarea name="comment" rows="5" cols="50"></textarea></td> 
</tr> 

<tr> 
<td colspan="2" align="center"><input type="reset" name="reset" value="RESET" /> 
<input type="submit" name="submit" value="SUBMIT" /></td> 
</tr> 
</table> 
</form>
Now our form is set to POST and forwards all the data to add.php which is where all the storing input to the database will happen.

Create another page called, "add.php"
Code:
<?php 
function safeAddSlashes($string) { 
if (get_magic_quotes_gpc()) { 
return $string; 
} else { 
return addslashes($string); 
} 
} 

function mailcheck($emailadr){ 
if (eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$", 
$emailadr)) 
return $emailadr; 
else 
return false; 
} 

$name = $_POST['name']; 
$email = $_POST['email']; 
$comment = $_POST['comment']; 
$datetime = date("Y-m-d"); 

$comment = safeAddSlashes($comment); 
$email = mailcheck($email); 

$dbHost = "localhost"; /* change this to your database hostname */
$dbUser = ""; /* Your Username used for accessing the database */
$dbPass = ""; /* Your Password for the database */
$dbname = "guestbook"; /* Your Database Name */

$db = mysql_connect($dbHost,$dbUser,$dbPass); 
mysql_select_db($dbname,$db); 

$sql="INSERT INTO guestbook ('name', 'email', 'dateposted', 'comment') 
VALUES ('$name', '$email', '$datetime', '$comment')"; 

$result = mysql_query($sql, $db); 

if ($result) { 
echo 'Thank you, your message has been entered successfully<br />'; 
echo 'To view the guestbook click <a href="index.php" 
title="View Guestbook">here</a>.'; 
} else 
echo 'Your message could not be added.'; 
?>
The first strange thing you may notice with this code is the JavaScript function at the top, well I will explain to you what it does:

This function is for protection
Code:
function safeAddSlashes($string) { 
if (get_magic_quotes_gpc()) { 
return $string; 
} else { 
return addslashes($string); 
} 
}
------------
This is the function to check if the users email address is valid, is splits up the email address into 2 parts, the text before the @ and the bit after it to make sure a valid email is entered, if it is valid it will be sent along, if it is not then a NULL value will be sent.

Code:
function mailcheck($emailadr){ 
if (eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$", 
$emailadr)) 
return $emailadr; 
else 
return false; 
}
The next step is creating the output of the guestbook, since this is only a tutorial I won't make it look spectacular, I will leave it basic so you can edit it later on.

Create another page called, "view.php"
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 

<title></title> 

</head> 

<body> 

<h3>View Guestbook</h3> 

<?php 
$dbHost = "localhost"; /* change this to your database hostname */
$dbUser = ""; /* Your Username used for accessing the database */
$dbPass = ""; /* Your Password for the database */
$dbname = "guestbook"; /* Your Database Name */

$db = mysql_connect($dbHost,$dbUser,$dbPass); 
mysql_select_db($dbname,$db); 


$requete = "SELECT ID, name, email, dateposted, comment 
FROM guestbook ORDER BY ID desc"; 
$result = mysql_query ($requete,$db); 

while($row = mysql_fetch_assoc($result)) 
{ 
$name = $row['name']; 
$email = $row['email']; 
$comment = $row['comment']; 
$datetime = $row["dateposted"]; 

if ($email) 
echo '<a href="mailto:'.$email.'">'; 
echo $name; 
if ($email) 
echo '</a>'; 
echo ' - Posted on '.$datetime.'<br />'; 
echo ''.$comment.''; 
echo '<hr />'; 
} 
?> 

</body> 
</html>
Now you will see in this code, when I am fetching the fields from the table I am calling them up in ORDER BY ID desc. When the data is sent to the database we have our fieldname ID give it a number, for the first ever record it would be 1, the next record would then be 2 and so on. When we call them in order by ID desc we call up the most recently added data first. Say 20 people have signed the guestbook, number 20 would be the first shown record, then 19, 18, 17 ect ect.

Now In the folder of your guestbook, create a file called "index.php"
Add your layout code, and In the content section, add this:
Code:
<?php 
include('send.php');
echo "<br />";
include('view.php');
?>
Just, View your index.php, and Look! Your First Guestbook, Post A Test Comment in it, And Check if it works. If you have any problems, PM me.

(Tutorial Still In Development)
__________________
I love Pokemaster
http://deoland.myminicity.com/
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 04:55 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Template-Modifications by TMS
© 1999-2009 PokeZam. All Rights Reserved.