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

AABU GTUG Opening Event

On February 17, 2011 We’ve headed to Al Al Bayt University, to run one of the biggest event established there, for opening the AABU GTUG (Google’s Technologies Users Group). The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees exceeded 250 attendee, in addition to Vice president of Al Al Bayt University Dr. Hashem Al Masaeed, Deanship of College of I.T at Al Al Bayt University Prof. Ismail Ababneh, and a quite number of professors and teachers at the college of I.T at AABU, in addition to Yarmuk fm and Al Ro’aya  tv. AABU GTUG as a Google’s Technologies Users Group interested in Google’s technologies and tries as possible to increase students awareness about Google’s technologies, and introduce it to them. Their was a lot of activities during the event, beginning with the key note from me Anas Jaghoub, that included introducin...

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

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

سلسلة تعلم برمجة تطبيقات الاندرويد – إنشاء أول تطبيق للاندرويد – الحلقة رقم 2

تحدثنا في الحلقة السابقة عن إعداد بيئة العمل، وأهم الأدوات والحزم اللازمة للبدء في تطوير تطبيقات لنظام التشغيل الاندرويد. سنتحدث في هذه الحلقة عن كيفية إنشاء تطبيق للاندرويد، وما هي مكونات تطبيق الاندرويد والبنية الهيكلية ﻷي تطبيق على الاندرويد. إنشاء تطبيق باستخدام بيئة العمل Netbeans : 1. اضغط على قائمة File 2. اختر New Project. 3. اختر نوع المشروع Android 4. اضغط زر Next. 5. اضبط إعدادات المشروع كالتالي: Project Name: اسم المشروع، يفضل أن يكون اسم المشروع هو نفس اسم التطبيق وذلك لتمييزه عن باقي المشاريع الأخرى Package Name: اسم حزمة المشروع، وهنا يجب اتباع قواعد تسمية الحزم في لغة Java إذ يجب أن تتكون من مقطعين على الأقل ويفصل بين كل مقطع بنقطة، ولا يوجد فراغات. Target Platform: رقم نسخة نظام التشغيل الاندرويد التي يدعمها هذا التطبيق، وهنا لا بد من اختيار أقل رقم يتوافق معه هذا التطبيق لمزيد من التفاصيل شاهد الصورة التالية: 6. اضغط على زر Finish وهكذا نكون قد أنشأنا بمساعدة بيئة العمل Netbeans أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...