You are on page 1of 9

Web Development inPHP with Database application

1. Start the Server e.g. XAMPP


a. Look for XAMPP folder and open it e.g. C:\XAMPP
Run XAMPP-Control.exe
Or Look for the shortcut (icon) of XAMPP in the desktop and double click to open
b. Click Apache and MySQL Action Start button

c.
d. Open a web browser then type: http://localhost/phpmyadmin
e. If access denied error is encountered, do the following: (If no error, proceed to Step i.)

f. Type me inside the quote e.g. ‘me’


g. Stop the Server and Start again.
h. Open a web browser then type: http://localhost/phpmyadmin
i. If no error, below will be the result

Page 1 of 9
======================================================================================================
Objectives:Creating a simple PHP Database Application namedGUESTBOOK_DB.
Overview:
In this Activity, youneed to create a database GUESTBOOK_DB and3 PHP files.
1. guestbook.php
2. addguestbook.php
3. viewguestbook.php

STEPS:

STEP 1: Set up database


A. Create a database named "GUESTBOOK_DB" with table named “GUESTBOOK”.

1. Click the Database tab to create a database.


2.

3. Type GUESTBOOK_DB in the text box Database name


4. Click Create button
5. Then, create a table named GUESTBOOK with 5 columns

6.
7. Then Click Go (far right)
8. Open SQL tab then click Clear button. Then, type the below SQL statement.

Below is the GUESTBOOK table structure:

CREATE TABLE `guestbook` (


`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
)

Page 2 of 9
9. Once done typing, click Go (far right)
10. Click STRUCTURE tab to see the table structure (GUESTBOOK).

11.

B. Create the PHP files

1. Create file guestbook.php.


2. Create file addguestbook. php.
3. Create file viewguestbook.php

==================================================================================================
STEP2: Create the file guestbook.php via Notepad++ (or any text editor)

Page 3 of 9
<!DOCTYPE HTML>
<HTML>
<HEAD><TITLE>My Guest Book PHP file</TITLE></HEAD>
<BODY>

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">


<tr>
<td><H2> Sign Guestbook </H2></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /><input type="reset" name="Submit2" value="Reset"
/></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><H2><a href="viewguestbook.php">View Guestbook</a></H2></td>
</tr>
</table>

<BODY>
</HTML>
Note: Save the file in XAMPP/htdocs folder
Page 4 of 9
Then open a web browser, then type, http://localhost/guestbook.php

STEP 3: Create file addguestbook.php

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="me"; // Mysql password - if needed make this blank ""
$db_name="guestbook_db"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.


//$conn = mysqli_connect("$host", "$username", "$password");

$conn = new mysqli($host, $username, $password, $db_name);

// Check connection
if (!$conn) {
die("Connection to server failed!".mysqli_connect_error());
}
echo "Connected successfully!";

//$db = mysqli_select_db($conn,"$db_name");

// variable declaration
$datetime=date("y-m-d h:i:s"); //date time
$name = ($_POST["name"]); //name
$email = ($_POST["email"]); // email
$comment = ($_POST["comment"]); //comment

$sql="INSERT INTO $tbl_name(name, email, comment, datetime) VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysqli_query($conn,$sql);

//check if query successful


if($result){
echo "Successful";
echo "<BR>";

// link to view guestbook page


echo "<a href='viewguestbook.php'><strong>View Guestbook</strong></a>";
}

else {
echo "ERROR";
}
mysqli_close($conn);
?>

Page 5 of 9
TO add new user in the database,
1. Type in the web browser: http://localhost/phpmyadmin/server_privileges.php
2. In User accounts tab, Click Add user account.

3. In User name, type me. In Password type, me. In Re-type, type me. Click Check All in Global

Privileges. Note: In Host Name, select


Local
4. Scroll down then click Go (far right).

Page 6 of 9
STEP 4: Create file viewguestbook.php

<!DOCTYPE HTML>
<HTML>
<HEAD><TITLE>My Guest Book PHP file</TITLE></HEAD>
<BODY>

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">


<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a></strong></td>
</tr>
</table>
<br>

<?php

$host="localhost"; // Host name


$username="root"; // Mysql username
$password="me"; // Mysql password - if needed make this blank ""
$db_name="guestbook_db"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.


//$conn = mysqli_connect("$host", "$username", "$password");

$conn = new mysqli($host, $username, $password, $db_name);

// Check connection
if (!$conn) {
die("Connection to server failed!".mysqli_connect_error());
}

//$db = mysqli_select_db($conn,"$db_name");

$sql="SELECT * FROM $tbl_name";


//$result=mysql_query($sql);

$result = mysqli_query($conn,$sql);

while($rows=mysqli_fetch_array($result)){

?>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">


<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
Page 7 of 9
</tr>
<tr>
<td>ID</td>
<td>:</td>
<td><?php echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><?php echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><?php echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><?php echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>

<?php
}
mysqli_close($conn);//close database
?>

<BODY>
</HTML>

Once the 3 PHP files are created, open guestbook.php in the web browser then try to insert at least 10 records.
Start playing around on the Guestbook PHP database application.

Note : Feel free to modify your web design and other content. Squeeze your creative juices!!!

TODelete a record, open http://localhost/phpmyadmin. Then, select Database tab. Select a database
to open (e.g. guestbook_db) – double click to open.
Select SLQ tab then type SELECT * FROM GUESTBOOK ->(this is to view the records)
Then, Click Go.
The Browse tab will be opened where in the added list of records is displayed.
Page 8 of 9
You can delete a record if needed. Click the red minus sign before Delete.

Page 9 of 9

You might also like