Академический Документы
Профессиональный Документы
Культура Документы
On
PHP
Submitted for partial fulfillment Of B. Tech.
in
COMPUTER SCIENCE AND ENGINEERING
SUBMITTED TO: Er.Ankush (Lect in CSE/IT Deptt.) SUBMITTED BY:Hitender Dalal 1609068 CSE 4
th
Year
CERTIFICATE
I hereby certify that the work presented in this report entitled PHP submitted in the partial fulfillment of the requirement for the award of degree of Bachelor from Computer Science Engineering, from JIND INSTITUTE OF TECHNOLOGY,JIND and is an authentic record of my own work.
ACKNOWLEDGEMENT
With profound sense of gratitude, I take it as a highly esteemed privilege in expressing my sincere thanks to my supervisor and guide Miss SUMAN for her technical guidance, sound advice, excellent supervision, valuable suggestions and ever available help during entire course of training. Without her wise counsel and able guidance, this training would not have been completed in this manner. I owe my sincere gratitude to all the faculties of CSE Jind Institute of Engineering and Technology, Jind for their encouragement and unfailing interest in sincere suggestions from time to time in this work. Finally, I owe to the almighty who gave me to carry out this work with sincerity and dedication.
TABLE OF CONTENTS
1. INTRODUCTION....................................................................................................6-7 1.1 HISTORY OF PHP..........................................................................................6 1.2 FEATURES OF PHP.......................................................................................7 2. HTML........................................................................................................................8-9 2.1 HYPERTEXT MARKUP LANGUAGE.......................................................8 2.2 TAGS IN HTML...............................................................................................8 2.3 FEATURES OF HTML...................................................................................9 2.4 BREAKS USED IN HTML.............................................................................9 3. KEY CONCEPTS OF HTML.................................................................................10-11 3.1 ELEMENTS.....................................................................................................10 3.2 TAGS................................................................................................................10 3.3 ATTRIBUTES NAD VALUES......................................................................10 3.4NESTING..........................................................................................................11 4. TAGS AND CODING...............................................................................................12-15 5. PHP.............................................................................................................................16-17 5.1 HYPERTEXT PRE-PROCESSOR................................................................16 6. CONTROL STRUCTURE IN PHP.........................................................................18-30 6.1 CONDITIONAL CONTROL STRUCTURE................................................18 6.2 UNCONDIONAL CONTROL STRUCTURE...............................................26 6.3 REPEATITION LOOPS...................................................................................28 7. $GET AND $POST VARIABLES.............................................................................31-32 7.1 $GET VARIABLE............................................................................................31 7.2 $POST VARIABLE...........................................................................................31 7.3 DIFFERENCE BETWEEN GET AND POST................................................32 8. SESSION , COOKIES AND FILE UPLOADING....................................................33-35 8.1 PHP SESSION VARIABLE...............................................................................33 8.2 STARTING A PHP SESSION............................................................................33 8.3 PHP COOKIE.......................................................................................................33 8.4 CREATING A PHP COOKIE............................................................................34 8.5 RETRIVING A COOKIE...................................................................................34 8.6 DELETING A COOKIE.....................................................................................34 8.7 PHP FILE UPLOADING....................................................................................35 8.8 CREATING AN UPLOAD FILE.......................................................................35
9. JAVASCRIPT...............................................................................................................36-37 9.1 INTRODUCTION TO JAVASCRIPT...............................................................36 9.2 JAVASCRIPT FORM VALIDATION...............................................................37 9.3 REQUIRED FEILDS............................................................................................37 10. CASCADING STYLE SHEETS (CSS).....................................................................38-39 10.1 INTRODUCTION TO CSS................................................................................38 10.2 STYLE SHEET...................................................................................................38 10.3 EXTERNAL STYLE SHEET............................................................................38 10.4 INTERNAL STYLE SHEET.............................................................................39 10.5 INLINE STYLES................................................................................................39 11. MySQL DATABASE...................................................................................................40-54 11.1 MySQL DATABASE..........................................................................................40 11.2 CONNECTING A DATABASE........................................................................44 11.3 PHP MySQL INSERT INTO.............................................................................45 11.4 PHP MySQL SELECT.......................................................................................48 11.5 DISPLAY RESULT IN AN HTML TABLE....................................................49 11.6 PHP MySQL THE WHERE CLAUSE.............................................................50 11.7 PHP MySQL ORDER BY KEYWORD............................................................51 11.8 PHP MySQL UPDATE.......................................................................................52 11.9 PHP MySQL DELETE.......................................................................................54 12. REFRENCES...................................................................................................................55
1. INTRODUCTION
PHP stands for HYPERTEXT PRE-PROCESSOR . But It stands for HPP then why it still called PHP . We came to know about it in the history of PHP.
SYNTAX OF PHP
< ? php
?>
2. HTML
2.1 HYPER TEXT MARKUP LANGUAGE
HYPER - non linear TEXT - any simple text MARKUP - tags and linking LANGUAGE - any language for programming. It is used to generate web page . HTML, an initialism of Hypertext Markup Language, is the predominant markup language for web pages. It provides a means to describe the structure of textbased information in a document by denoting certain text as headings, paragraphs, lists, and so on.
3.1 ELEMENTS
All HTML pages are made up of elements. Think of an element as a container in which a portion of a page is placed. Whatever is contained inside the element will take on the characteristics of that element. For example, to identify a heading on a page, you would enclose it in a heading element <h1> </h1>. If you want to create a table, you put the table information inside the table element <table> </table>. To construct a form, you need the form element <form> </form>.
3.2 TAGS
Often, youll find the terms element and tag used interchangeably. Its fairly common, but not strictly accurate. An element is made up of two tags: an opening tag and a closing tag. Although it might seem somewhat picky to make this distinction, when you begin to work with XHTML (Extensible Hypertext Markup Language), it will be a very important difference to remember. If you get into the habit of distinguishing elements and tags from the very beginning, youll save yourself some confusion down the line. All tags are constructed the same way. The tag begins with a less than sign (<), then the element name, followed by a greater than sign (>). For example, an opening tag for the paragraph element would look like this: <p>. The only difference in a closing tag is that the closing tag includes a slash (/) before the element name: </p>. Your content goes between the tags. A simple paragraph might look like this: <p>This is an HTML paragraph.</p> Some elements do not use closing tags because they do not enclose content. These are called empty elements. For example, the line break element <br> does not require a closing tag. In the case of empty elements, add a closing slash after the element name, like this: <br />. When a browser sees the slash, it will recognize the element as one that does not need a separate, closing tag.
10
Be sure to notice that an equals sign and a set of quotation marks follow both the height and the width attributes. Thats because attributes need values to go with them. In the case of the preceding illustration, you might add a value of 200 to cause your image to display at a size of 200 x 200 pixels:
<img height="200" width="200" />
values work together with attributes to complete the definition of an elements characteristics. An easy way to think of how attributes and values work together is to compare them with nouns and adjectives. A noun names something; an adjective describes it. An attribute names a characteristic; a value describes it. Imagine that you are trying to identify a persons hair color with a markup language. Hair would be the element, color the attribute, and red the value. You might write such a description as follows:
<hair color="red">Red-headed Person</hair>
3.4 NESTING
Often you will want to apply more than one element to a portion of your page. An essential concept to understand is nesting. Nesting simply means that elements must never overlap. Properly nested elements are contained inside one another, as in the following:
<a> <b> <c> </c> </b> </a>
Sometimes its easier to understand the concept if the elements are displayed vertically, like this:
<a><b><C> </c> </b> </a>
Web browsers displaying an HTML page can be pretty forgiving if your elements are not properly nested; however, overlapped elements can create garbled results, particularly if you are trying to construct frames or tables. Also, when you become familiar with XHTMLs stricter standards, youll discover that overlapping elements are an absolute no-no.
11
<font> : Text can also be written in this tag with more features than h1 tag . We can provide different attributes in this tag : size color face
<div> : It is used for page division . It creates a new section in the Webpage . Mainly used in CSS styling . <p> : It is used for more than one line break and text can also be written in this text . <b> : It is used to make text bold
12
<i> : It is used to make text italic <u> : It is used to underline the text <a> : It stands for anchor tag and used to give links in it . Another page link can be given in this tag . <img> : It is used to add image in the webpage . It have different attributes : align height width
<table> : It is used to create the table in the Webpage . It have following attributes align height width bgcolor border bordercolor cellpadding cellspacing
<th> : It stands for table header . This tag is used in table tag . <tr> : It stands for table row . This tag is used in table tag <td> : It stands for table data . This is used in table tag
13
<form> : It is used for creating an HTML form of user input . It can contain one or more of the following form elements : <input> <textarea> <button> <select> <option> <optgroup> <feildset> <label> It have following attributes : accept accept - charset action autocomplete enctype method name target novalidate
Type of input in form tag : login password checkbox radio image button
14
submit reset <frameset> : HTML frames allow authors to present documents in multiple views, which may be independent windows or subwindows . Attribute of this tag are : rows cols
<frame> : Source for the frame is given in this tag . <iframe> : It is used to give a fixed frame space in a Webpage . Attributes of iframe are : height width align scrolling name
<ul> : It stands for unordered list . Attributes are : circle square fill circle
<li> : It is used for listing items in ordered and unordered lists . <bgsound> : This tag is used for adding music files .
15
5. PHP
5.1 HYPERTEXT PRE - PROCESSOR
1) In PHP we can't use any data types . 2) In PHP we use $ symbol before variable initialization . 3) In PHP we use three statement to print the output . print : It is used to print single statement . printf : It is used to print numeric value . echo : It is used for both purposes .
4) PHP configuration file is php.ini 5) We save PHP files in wamp > www > .......
A simple program
< ? php $a = 10 ; $b = "c" ; $c = "Hello Welcome To PHP" ; echo $a , "<br>" ; echo $b , "<br>" ; echo $c , "<br>" ;
?>
16
17
if :
In conditional control structures we have only one condition and one statement . If the condition is true then the statement is printed else statement is terminated . syntax : if (condition) { statement ; } < ? php $a = 10 ; if ($a > 0) { echo " $a is greater than zero " ; } ?>
18
19
if else :
In this we have one condition but two statements . If the condition is true then the statement 1 is printed and if condition is false statement 2 is printed in the output . syntax : if (condition) { statement 1 ; } else { statement 2 ; } <?php $a=-10; if($a>0) { echo"$a is greater than zero"; } else { echo"$a is smaller than zero"; } ?>
20
21
nested if : If within a if is called nested if . If our outer condition is true then control goes to inner if part else control goes to if - else part . <?php $a=20; $b=30; $c=10; if($a>$b) { if($a>$c) { echo"$a is greater"; } else { echo"$c is greater"; } } else if($b>$c) { echo"$b is greater"; } else {
22
23
ladder if : There are if statements one after another forming a if - else ladder . <?php $marks=75; if($marks>90) { echo"grade-A"; } else if($marks>75) { echo"grade-B"; } else if($marks>60) { echo"grade-C"; } else { echo"student fail"; } ?>
24
25
<?php $ch='t'; switch($ch) { case 'm': echo"monday"; break; case't': echo"tuesday"; break; case 'w': echo"wednesday"; break; deafault: echo"invalid"; } ?>
26
27
28
do while : First statement is executed then condition is checked . Any statement will be executed atleast once. <?php $a=1; do { echo $a,"<br>"; $a++; } while($a>10); ?>
29
30
changed by setting the post_max_size in the php.ini file). Example <form action="welcome.php" method="post"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> When the user clicks the "Submit" button, the URL will look like this: http://www.w3schools.com/welcome.php The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array): Welcome <?php echo $_POST["fname"]; ?>!<br /> You are <?php echo $_POST["age"]; ?> years old
32
33
In the example below, we retrieve the value of the cookie named "user" and display it on a page: <?php // Print a cookie echo $_COOKIE["user"]; // A way to view all cookies print_r($_COOKIE); ?>
34
35
9. JAVASCRIPT
9.1 INTRODUCTION TO JAVASCRIPT
JavaScript is THE scripting language of the Web. JavaScript is used in billions of Web pages to add functionality, validate forms, communicate with the server, and much more. JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight programming language JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary
Example: <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <script type="text/javascript"> document.write("<p>My First JavaScript</p>"); </script> </body> </html>
36
37
Database Tables A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data. Below is an example of a table called "Persons": LastName FirstName Address Hansen Ola Svendson Tove PettersenKari City
The table above contains three records (one for each person) and four columns (LastName, FirstName, Address, and City).
Queries A query is a question or a request. With MySQL, we can query a database for specific information and have a recordset returned. Look at the following query: SELECT LastName FROM Persons
40
The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this:
LastName Hansen Svendson Pettersen MySQL is the most popular open-source database system.
Create Database and Tables A database holds one or multiple tables. Create a Database The CREATE DATABASE statement is used to create a database in MySQL. Syntax: CREATE DATABASE database_name To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example The following example creates a database called "my_db": <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }
{ echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?>
Create a Table The CREATE TABLE statement is used to create a table in MySQL. Syntax: CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... ) To learn more about SQL, please visit our SQL tutorial. We must add the CREATE TABLE statement to the mysql_query() function to execute the command. Example The following example creates a table named "Persons", with three columns. The column names will be "FirstName", "LastName" and "Age": <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) {
42
// Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); }
// Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )";
mysql_close($con); ?> Important: A database must be selected before a table can be created. The database is selected with the mysql_select_db() function.
43
Note: When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(15). mysql_query($sql,$con);
Example In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error());
44
Closing a Connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }
// some code
mysql_close($con); ?>
45
The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
To get PHP to execute the statements above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example In the previous chapter we created a table named "Persons", with three columns; "Firstname", "Lastname" and "Age". We will use the same table in this example. The following example adds two new records to the "Persons" table: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("my_db", $con);
mysql_close($con); ?> Insert Data From a Form Into a Database Now we will create an HTML form that can be used to add new records to the "Persons" table. Here is the HTML form:
46
<html> <body> <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>
</body> </html> When a user clicks the submit button in the HTML form in the example above, the form data is sent to "insert.php". The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables. Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the "Persons" table. Here is the "insert.php" page: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error());
47
mysql_select_db("my_db", $con);
echo "<br />"; } mysql_close($con); ?> The example above stores the data returned by the mysql_query() function in the $result variable. Next, we use the mysql_fetch_array() function to return the first row from the recordset as an array. Each call to mysql_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']). The output of the code above will be: Peter Griffin Glenn Quagmire
{ echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> The output of the code above will be: Firstname Lastname Glenn Peter Quagmire Griffin
specified criterion.
{ die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'");
while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } ?> The output of the code above will be: Peter Griffin
mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons ORDER BY age"); while($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br />"; } mysql_close($con); ?>
The output of the code above will be:
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
52
To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example Earlier in the tutorial we created a table named "Persons". Here is how it looks: FirstName LastName Age Peter Glenn Griffin 35 Quagmire 33
The following example updates some data in the "Persons" table: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("UPDATE Persons SET Age=36 WHERE FirstName='Peter' AND LastName='Griffin'"); mysql_close($con); ?> After the update, the "Persons" table will look like this: FirstName LastName Age Peter Glenn Griffin 36
Quagmire 33
53
The following example deletes all the records in the "Persons" table where LastName='Griffin': <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); mysql_close($con); ?>
54
12. REFRENCES
1. http://www.w3schools.com/html/default.asp 2. http://en.wikipedia.org/wiki/HTML 3. http://en.wikipedia.org/wiki/PHP 4. http://php.net/manual/en/index.php 5. http://www.w3schools.com/php/default.asp 6. http://www.w3schools.com/js/default.asp 7. http://en.wikipedia.org/wiki/JavaScript 8. www.w3schools.com/css/ 9. en.wikipedia.org/wiki/Cascading_Style_Sheets 10. en.wikipedia.org/wiki/MySQL 11. www.w3schools.com/php/php_mysql_intro.asp 12. www.w3schools.com/php/php_sessions.asp 13. php.net/manual/en/features.sessions.php 14. php.net/manual/en/function.setcookie.php 15. www.w3schools.com/php/php_cookies.asp 16. php.net/manual/en/features.file-upload.php 17. www.w3schools.com/php/php_file_upload.asp 18. www.w3schools.com/tags/tag_html.asp
55