Skip to main content

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

Comments

Popular posts from this blog

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-based.

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

Web Development Best Practices (PHP Best Practices)

Websites development is one of the most challenging projects ever to develop, according to the large number of components you deal with, integrate, and manage in your application. For example, most web projects Deal with databases as their data source, and I ntegrate with external services called web services and APIs. As well the application M anages its own objects and components from views to models to controllers to plugins and so on. A skilled web developer should have a great/deep knowledge in most of components used in his system, and as well should ha ve intensive focus on how each component work and how can be used, what can be done, and what cannot. On the other hand, the huge amount of requirements, components to manage, and time frame given to you to accomplish your mission, and number of people in your team, all these factors together requires from you to organize your tasks, and priorities your requirements, do deep analysis and planning in order to have a hi