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.
$form=‘<form name=“employee” method=“get” action=“employee.php”>
Employee No: <input type=“text” name=“id”><Br>
<input type=submit value=“Go”>
// last modified 13/oct/2009
$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>”;
$row = mysql_fetch_array($result);
echo $row[‘department’] .”<br>”;
echo $row[‘jobTitle’] .”<br>”;
echo $row[‘salary’] .”<br>”;
echo $row[‘telephone’] .”<br>”;
echo $row[‘address’] .”<br>”;
echo $row[‘cv’] .”<br>”;
•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
•After submitting the form, URL will look like this: yourdomain/employee.php?id=“value of id field”
•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.
•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