Advanced Internet Development Dynamic Web pages with php and MySQL Lecturers Dr Phil Davies Mr Keith Norris
Basic Building Blocks Web Dbase Browser Web Server Web Application Server MySQL Database MySQL RDBMS $conn = mysql_connect(“host”, “user”, “password”); $rs = mysql_select_db($database, $conn) or die(“can’t get into database”); $res1 = mysql_query($query_string); mysql_close($conn);
Development of a Dynamic Web Page info.html Student Number? MySQL Database holding Student Records find_student.php Web Server Apache Search for a Particular Student via Student Number and display it student.html Input Data for DBase Student Name Forename Surname Age Description php Web Application Server student_insert.php Actioned from student.html form input to Insert: Student Name Forename Surname Age Description
Develop a Tutorial that will guide a student at this level through the process of developing such a dynamic web server based system
Setting up Apache/php/MySQL Environment – slight changes Run XAMPP Control Panel Run Apache and MySQL Server Click on Admin – Apache or use localhost:8080 phpMyAdmin > following screen shots mix between XAMPP and Uniform Server
phpMyAdmin
Create a Table Create a Table called student_rec in database student_info_db This will have 5 fields: s_number; s_forename; s_surname; s_age; s_description
Create Table and Populate Record Layout
Table Structure Confirmed To insert student records
Create a Student Record
Create Second Student Record
Search to Check Records
Results of Search
Print View of Results
Development of a Dynamic Web Page info.html Student Number? MySQL Database holding Student Records find_student.php Web Server Apache Search for a Particular Student via Student Number and display it student.html Input Data for DBase Student Name Forename Surname Age Description php Web Application Server student_insert.php Actioned from student.html form input to Insert: Student Name Forename Surname Age Description
student.html – insert a record Will ACTION student_insert.php
Student.html – form for input <head> <title> This is Inputting a Student Record </title> </head> <body> <h1> Form to Permit The Insertion of a New Student</h1> <form method = "post" action = "http://localhost/student_insert.php"> <p><label>Student Number:<input type = "text" name = "s_number" size= "8" maxlength = "8" /></label></p> <p><label>Student Forename:<input type = "text" name = "s_forename" size= "15" maxlength = "15" /></label></p> <p><label>Student Surname:<input type = "text" name = "s_surname" size= "25" maxlength = "25" /></label></p> <p><label>Student age:<input type = "text" name = "s_age" size= "3" maxlength = "3" /></label></p> <p><label>Student Description:<input type = "text" name = "s_description" size= "50" maxlength = "50" /></label></p> <input type= "submit" value = “Insert Record"> </form> </body> </html>
Development of a Dynamic Web Page info.html Student Number MySQL Database holding Student Records find_student.php Web Server Apache Search for a Particular Student via Student Number and display it student.html Input Data for DBase Student Name Forename Surname Age Description php Web Application Server student_insert.php Actioned from student.html form input to Insert: Student Name Forename Surname Age Description
student_insert.php <?php extract($_POST); $username = "root"; $password = ""; $database = "student_info_db"; $host = “localhost”; $conn = mysql_connect($host, $username, $password) or die("sorry"); $rs = mysql_select_db($database, $conn) or die(“can’t get into database”); $query = "INSERT INTO student_rec (s_number, s_forename, s_surname, s_age, s_description) VALUES ('$s_number', '$s_forename', '$s_surname', '$s_age', '$s_description')"; $result = mysql_query( $query) or die ("could not insert record query"); mysql_close($conn); ?>
phpMyAdmin search (3 records)
Development of a Dynamic Web Page info.html Student Number> MySQL Database holding Student Records find_student.php Web Server Apache Search for a Particular Student via Student Number & Display it student.html Input Data for DBase Student Name Forename Surname Age Description php Web Application Server student_insert.php Actioned from student.html form input to Insert: Student Name Forename Surname Age Description
info.html <html> <head> <title> Searching for a Student Record via Student Number</title> </head> <body> <h1> Searching for a Student via Student Number</h1> <form method = "post" action = "http://localhost/find_student.php"> <p><label>Student Number:<input type = "text" name = "s_number" size= "8" maxlength = "8" /></label></p> <input type= "submit" value = "Find Student"> </form> <p> </body> </html>
Development of a Dynamic Web Page info.html Student Number? MySQL Database holding Student Records find_student.php Web Server Apache Search for a Particular Student via Student Number & Display it student.html Input Data for DBase Student Name Forename Surname Age Description php Web Application Server student_insert.php Actioned from student.html form input to Insert: Student Name Forename Surname Age Description
Student.php – display all $username = "root"; $database = "student_info_db"; $host = "localhost"; $password = ""; $conn = mysql_connect($host,$username,$password) or die("sorry"); $rs = mysql_select_db($database, $conn) or die ("sorry - could not connect to the database"); $query = "SELECT * from student_rec"; $res = mysql_query($query); $num_rows = mysql_num_rows($res); for ($i=0; $i<$num_rows; $i++) { $row_array = mysql_fetch_row($res); echo “Student Record Holds “.$row_array[0].”**”.$row_array[1] ].”**”.$row_array[2] .”**”.$row_array[3].”**”.$row_array[4]."<br/>"; } mysql_close($conn); ?>
Display all records (SQL)
Select a Single Student (SQL) find_student.php extract($_POST); $username = "root"; $password = ”"; $database = "student_info_db"; $conn = mysql_connect("localhost",$username,$password) or die("sorry"); $rs = mysql_select_db($database, $conn) or die(“can’t get into database”); $query = "SELECT s_number, s_forename, s_surname, s_age, s_description FROM student_rec WHERE s_number = '". $s_number ."'"; $result = mysql_query($query) or die("could not do query"); if ($result) { $row_array = mysql_fetch_row($res); echo “Student Record Holds “.$row_array[0].”**”.$row_array[1] ].”**”.$row_array[2] .”**”.$row_array[3].”**”.$row_array[4]."<br/>"; } else{ echo "no rows"; mysql_close($conn); ?>
Dynamic Web Page with a bit of style <html> <head> <title> Find a Particular Student </title> <style type="text/css"> p {text-align:center;color:red} h1 {text-align:center;color:blue} </style> </head> ........
External CSS <head> <title> Find a Particular Student </title> <link href="http://localhost/my_style.css" rel="stylesheet" type="text/css" /> </head> External File – my_style.css <style type="text/css"> p {text-align:center;color:red} h1 {text-align:center;color:blue} </style>
External CSS using php <head> <title> Find a Particular Student </title> <link href="http://localhost/my_style.php" rel="stylesheet" type="text/css" /> </head> External File – my_style.php p {text-align:center;color:red} h1 {text-align:center;color:green}
my_style.php <?php $grn = 'green'; ?> <style type="text/css"> p {text-align:center;color:red} h1 {text-align:center;color:<?php echo $grn;?>} </style>
Passing a Parameter to php stylesheet Link from html file <link href="http://localhost/my_style.php?param='green'" rel="stylesheet" type="text/css" /> -------------------------------------------------------------------------- <?php $grn = $_GET['param']; ?> <style type="text/css"> p {text-align:center;color:red} h1 {text-align:center;color:<?php echo $grn;?>} </style>
For your consideration? Can a variable be read from the mqsql file that will cause the web page style sheet to be amended so that a particular response will create a different presentation version of the page to be displayed for different users?