Monday, April 5, 2010

Exercise : 5 Database case study

For this exercise, we have chosen CASE B "PHP AND MYSQL". We have installed Xampp version 1.7.0 which contains distribution of Apache version 2.2.11, PHP v 5.2.8 and MySQL v5.1.30. Although the package contains phpMyAdmin v3.1.1, but for more comfort we are using SQLyog v5.22 as MySQL GUI.

1.

<HTML>
<HEAD>
<TITLE>
This is a test for server
</TITLE>
</HEAD>
<BODY>
<?php
echo "the remote address of the server is ", $_SERVER['REMOTE_ADDR'];
echo "<BR>";
echo "the name of the server is ", $_SERVER['SERVER_NAME'];
echo "<BR>";
echo "the location of the page is ", $_SERVER['PHP_SELF'];
?>
</BODY>
</HTML>


In this question, we have implemented $_SERVER['REMOTE_ADDR'], $_SERVER['SERVER_NAME'] and $_SERVER['PHP_SELF'] in localhost server and the results is shown in the screenshot below:


2.

<HTML>
<HEAD>
<TITLE>
Hello World
</TITLE>
</HEAD>
<BODY>
<?php
$myvar = "Hello World!";
echo $myvar;
?>
</BODY>
</HTML>

This code simply stores "Hello World" string in "myvar" variable and prints the variable. The screenshot is shown below.


3.

<HTML>
<HEAD>
<TITLE>
Hello World
</TITLE>
</HEAD>
<BODY>
<FORM METHOD="GET" ACTION="submit.php">
What's your name? <INPUT NAME="myname" SIZE=10>
(Then press ENTER)
</BODY>
</HTML>

This code creates a simple form which submit the text entered in the text to the submit.php page. The screenshot is given below.


4.

<HTML>
<HEAD>
<TITLE>
DISPLAY
</TITLE>
</HEAD>
<BODY>
<?php
echo "Hello, ", $_GET['myname'];
?>
</BODY>
</HTML>

This code saved under the file submit.php displays the text entered in the previous form which was passed through parameter in the header under the variable 'myname'. Here, we have to use $_GET['myname'] to extract the text posted from the form. Hence the screenshot is below


5.

To continue with inserting values in table, first we have create database which is assisted by the code "create database mydatabase" written in MySQL GUI. It is show in screenshot below:


Then after, we have to create table named "employees" with 4 fields. Screenshot below


Now,
a.

<HTML>
<HEAD>
<TITLE>
DISPLAY
</TITLE>
</HEAD>
<BODY>
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mydatabase',$db);
$result = mysql_query('SELECT * FROM employees',$db);
echo "First Name: ", mysql_result($result,0,'first'), "<BR>";
echo "Last Name: ", mysql_result($result,0,"last"), "<BR>";
echo "Address: ", mysql_result($result,0,"address"), "<BR>";
echo "Position: ", mysql_result($result,0,"position"), "<BR>";
?>
</BODY>
</HTML>

This code first connects mysql in localhost with "username=root" and "password=null" and then select the database named "mydatabase" which we created earlier and then displays the results of the first column in the table "employees". Here in this example i have inserted a sample data value which will be displayed as a result shown in the following screenshot.

b.

<HTML>
<HEAD>
<TITLE>
Add Record
</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="add_record.php">
First name:<INPUT TYPE="Text" NAME="first"><br>
Last name:<INPUT TYPE="Text" NAME="last"><br>
Address:<INPUT TYPE="Text" NAME="address"><br>
Position:<INPUT TYPE="Text" NAME="position"><br>
<INPUT TYPE="Submit" NAME="submit" VALUE="Enter information">
</BODY>
</HTML&gt;

This "add_record.html" page creates a form which submits values inserted in the textbox to the "add_record.php" page.

c.

<HTML>
<HEAD>
<TITLE>
Insert Record
</TITLE>
</HEAD>
<BODY>
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mydatabase',$db);
$result = mysql_query("INSERT INTO employees (first,last,address,position)
VALUES ('$_POST[first]','$_POST[last]','$_POST[address]','$_POST[position]')");
if ($result == 1) {
echo "Thank you! Your information has been entered."
} else {
echo "Sorry, theres a problem";
}
?>
</BODY>
</HTML>

This "add_record.php" page first connects to the database as described previously in step 5.a and then inserts the value into the table named "employees" with all the data posted from the form show in step 5.b. The message "Thank you ! Your information has been entered."
will be displayed if the data are successfully posted which is shown in the screenshot.


d.

<HTML>
<HEAD>
<TITLE>
Insert Record
</TITLE>
</HEAD>
<BODY>
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mydatabase',$db);
$result = mysql_query("SELECT * FROM employees",$db); echo "<table border=1>\n";
echo "<tr><td><b>Name</b></td><td><b>Position</b></tr>\n";
while ($myrow = mysql_fetch_row($result)) {
echo "<tr><td>", $myrow[2], ", ", $myrow[1], "</td><td>",
$myrow[4], "</td></tr>";
}
echo "</table><BR>";
?>
</BODY>
</HTML>

This "show_results.php" page shows the total results present in the table by applying "while" condition until the end of the total column in the table. The code creates a table for displaying the results. Screenshot below:

No comments:

Post a Comment