Guide for Building Web Application Using PHP and Mysql
Total Page:16
File Type:pdf, Size:1020Kb
Guide for Building Web Application Using PHP and Mysql 1. Download and install PHP, Web Server, and MySql from one of the following sites. You can look up the lecture notes on the class webpage for tutorials of HTML, PHP and more to create webpages as needed. 1. https://sourceforge.net/projects/wampserver/ 2. https://www.microsoft.com/web/webmatrix/ PHP is compatible with various web servers like Apache and the Microsoft’s IIS as well. All the PHP scripts are executed on the server (Apache, IIS etc) and it supports various databases like MySQL, Oracle, Solid, Generic ODBC etc; however, it is mostly used with MySQL. A PHP file has an extension of .phtml, .php or .php3 and it may contain various HTML tags, texts and scripts. Although a .php file contains scripts but when it is returned to the browser, it is returned as a plain HTML file. You can view a PHP file in Notepad as a source as well (right click on php file to select Edit with NotePad ). Note: Some MySql Commands in an old version are deprecated in the latest versions of WAMPServer, they will raise errors. mysq i are the new format for commands. Note that there may be version mismatch problems. Not all versions of PHP's will run with Apache2.4. For more info for this problem: http://forum.wampserver.com/read.php?2,119754,119754 Additional Guide to Set up: Edit Wamp Server icon ->PHP ->php.ini Look at phpinfo( ) for get your server information Example Codes in index.html, search_musicapp.php, search_musicapp1.php on the Class Webpage that implement the following Web Service Music Search. Example 1 of HTML and PHP Scripts for Search Music by Name and Type In the following 3 files (posted on the class web page) : To see source codes of php file, right click on the file, select Edit with Notepad or view as Source. Or you can open the folder as Web in VS. index.html search_musicapp.php search_musicapp1.php 1. Root Page in (index.html) shows menus in first page • APPS • MUSIC • View Transactions • Checkout • Logout 2. In the menu “MUSIC” will display the next page: Search Page (in search_musicapp.php) that takes Name of product to search and Category Type of product as input and Search button to submit. Search Enter Name : Select category Type: MUSIC APP Search Reset 3. Search button (in search_musicapp1.php ) when clicked will 1) Send the input to the Webserver, 2) Connect to MySql database, 3) Query over database with the input to search, 4) Display the result on the next page. The following Sample PHP Codes that implements a similar task with Search an employee with Name on the Company Website from Company database in MySql is given below: Build Search function in Company Website in root page to search an employee who is working for the company as follow: 1. Company Webpage in root page “idex.html” has Search in menu. (You can list more menu choice if you want). When Search link in the menu is clicked, the next page (SerachPage.php) is displayed. 2. In the next Search Page (in SerachPage.php) has Text input box to take Last Name of Employee and two Input Buttons “Search” and “Reset” 3. On Submit the button Search, the server script (in Search.php) will be executed to display the serach result in the next page. Serach.php will connect to your database, search Employee table to find an employee with the Last Name. Then on next page, it will display the employee information as output: First, Last Name, Sex, Dno of the employee in Table form. Sample codes for this Lab: 1. Create Index.html that shows all the home page menu including SEARCH menu with a href = “SearchPage.php” In index.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns ="http://www.w3.org/1999/xhtml" xml:lang= "en" lang ="en" > <head> <title> Company Home Page </title> <link href ="style.css" rel ="stylesheet" type ="text/css" /> <script src ="js/cufon-yui.js" type ="text/javascript" ></script> <script src ="js/cufon-replace.js" type ="text/javascript" ></script> <script src ="js/Bauhaus_Md_BT_400.font.js" type ="text/javascript" ></script> </head> <body id ="page1" > <div class ="tail-cont" > <div class ="tail-top-left" ></div> <div class ="tail-top" > <div class ="container" > <!-- header --> <div id ="header" > <div class ="logo" > <h3> Welcome to Company Website </h3> </a></div> <!--<div class="logo"><a href="index.html"><img src="images/logo.jpg" alt="" /></a></div> --> <ul class ="site-nav" > <li><a href ="index.html" >Home </a></li> <li><a href ="customer.html" >User </a></li> <li><a href ="admin.html" >Admin </a></li> <li><a href ="SearchPage.php" >SEARCH </a></li> <li class ="last" ><a href ="about-us.html" class ="act" >INFO </a></li> </ul> </div> <!-- content --> <div id ="content" > <div class ="indent" > <div class ="indent1" > </div> </body> </html> 2. SerachPage.php will be executed to display the following input boxes. In SerachPage.php, the script would look like this as below: <?php $con = mysqli_connect("localhost", "root", "") or die("Error connecting to database: ".mysqli_error()); mysqli_select_db($con, "company") or die(mysqli_error()); ?> <!DOCTYPE HTML> <html> <p><body> <h3>Search Company Database</h3> <p>Search by last name</p> <form method="post" action="search.php?go" id="searchform"> <input type="text" name="name"> <input type="submit" name="submit" value="Search"> </form> <form method="post" action="search.php" id="resetform"> <input type="submit" name="reset" value= "Reset"> </form> </body> </html> </p> 3. search.php will be executed to display the result on the next webpage. In search.php: The server script would look like this as below. <!DOCTYPE html> <html> <head> <title>Search results</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="style.css"/> </head> <body> <?php if (isset($_POST['name'])){ $query = $_POST['name']; echo $sql = mysqli_query($con, "SELECT * FROM employee WHERE (`LName` LIKE '%".$query."%')") or die(mysqli_error($con)); if (mysqli_num_rows($sql) > 0) { // if one or more rows are returned do following echo 'Fname Lname Sex DNo </br>'; while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) { echo $row['FName']. " " .$row['LName']." " .$row['Sex'] . " " .$row['DNo'] ."</br>"; } } else{ // if there is no matching rows do following echo "No results"; } } else{ echo 'Enter search conditions'; } ?> </body> </html> .