PHP Tutorial–Lesson 5: Introduction to PHP- Part III

PHP and MySQL

•Our previous application displays all employees in one web page, how about displaying information about one employee?

•To display information about a single employee we have to specify some information about this employee, such as his id.

•Our next application, will enhance the previous one, and improve it to display information for a single employee.

Example:

<?php

// inquery.php

$form=‘<form name=“employee” method=“get” action=“employee.php”>

Employee No: <input type=“text” name=“id”><Br>

<input type=submit value=“Go”>

</form>’;

echo $form;

?>

<?php

// Employee.php

// last modified 13/oct/2009

require(“../include/config.php”);

If(isset($_GET[‘id’]))

$id = $_GET[‘id’];

else die(“Unexpected value for employee id”);

$sql = “SELECT * FROM employee WHERE id=$id”;

$result = mysql_query($sql);

If(!$result) die(“MySql Error: <br>“.mysql_error());

If(mysql_num_rows($result) == 0)

echo “No information available<Br>”;

else

{

$row = mysql_fetch_array($result);

echo $row[‘id’].”<br>”;

$row[‘name’] .”<br>”;

echo $row[‘department’] .”<br>”;

echo $row[‘jobTitle’] .”<br>”;

echo $row[‘salary’] .”<br>”;

echo $row[‘telephone’] .”<br>”;

echo $row[‘address’] .”<br>”;

echo $row[‘cv’] .”<br>”;

}

?>

Notes:

•Inquery.php Script:

•It is a GUI screen, that allows you to view information about any employee by filling his id number.

•In this script I chose id, because it is unique, but you can inquiry using any other info related to the employee, such as his name, salary.

•Form method is get, because we want to get information from db.

•GET method propagate form data in the URL to the destination which is in our example: employee.php

•Before submitting the form, you might use your JavaScript skills to check whether the user has entered expected values or not, this will save time, and prevent server from hacking.

•After submitting the form, URL will look like this: yourdomain/employee.php?id=“value of id field”

•Employee.php Script:

•This script is an enhancement for the previous one in the last lesson, you can refer to it here: PHP Tutorial–Lesson 5: Introduction to PHP- Part II.

So am going to illustrate new things in it.

•$_GET[‘id’]: is a built-in array that holds variables propagated with URL. It is an associated array, and its key is variable name propagated with URL.

•Next we check for $_GET[‘id’] if it has value or not, by passing it to function isset. This will tell us whether the id has a value or not.

•If not. Then it will alert the user that id field is required and must be filled.

•In this case you might use javascript and follow single/double quotes rules, to make your application more user interactive.

•If $_GET[‘id’] has value then it seems to be that it had been set through your form. But not completely correct because it might be filled through hacking script. Anyhow, we expect that it had been filled through the form.

•You have to validate, each user inputs before entering it to database.

•Next, we check whether there is an employee whose id is the given id, using a built-in function offered by php which is mysql_num_rows() that returns an integer number of records that matches your query.

•We didn’t loop through all records, because it is only one record at most, and only for one employee, remember we restricted it with a unique value which is id.

End of Lesson 5, To download this lesson as PDF file click here: PHP Tutorial–Lesson 5: Introduction to PHP- Part III

Best Wishes,

Anas Jaghoub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s