Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

COMPROG2 – PHP & MYSQL DATABASE

CONNECTIVITY
Practice Exercise

INSTRUCTIONS:

• Please follow the filename format (minus 10 for wrong filename) File Structure:
• Make sure to use array and loops in your solution.
<root directory>/Students
• Create a similar file structure (found on the right).
css
• Create a database and name it as dbStudents. • styles_css.css

• Create a table named tblStudents inside dbStudents with the functions


following structure • config.php
• display_message.php

templates
• footer.php
• header.php

index.php

add_students_record.php

delete_students_record.php

edit_students_record.php
• Type the following codes saving it with the required filename and in search_students_record.php
the right folder (file structure/location)
view_students_record.php
• SCREENSHOTS of the OUTPUT can be found at the last page.

FILENAME: STYLES_CSS.CSS

1: table, th, td { 24:


2: border: 1px solid black; 25: table.message{
3: border-collapse: collapse; 26: width : 80%;
4: } 27: padding: 1px;
5: 28: border-spacing: 10px;
6: table.center { 29: word-spacing: 1;
7: margin-left: auto; 30: margin-left: auto;
8: margin-right: auto; 31: margin-right: auto;
9: } 32: /*background-color: red;*/
10: 33: }
11: table.banner{ 34:
12: background-color: gray; 35: td.message1{
13: width: 100%; 36: background-color: #FFCCCC;
14: padding: 10; 37: text-align: center;
15: } 38: font-family: 'Trebuchet MS', 'Lucida Sans
16: Unicode', 'Lucida Grande', 'Lucida Sans';
17: td.banner1{ 39: font-size: 20px;
18: font-family: 'Trebuchet MS', 'Lucida Sans 40: }
Unicode', 'Lucida Grande', 'Lucida Sans', 41:
'Arial', 'sans-serif'; 42: table.footer{
19: font-size: 48px; 43: width: 100%;
20: /*--color : #666666;*/ 44: background-color: #0000660;
21: text-align: center; 45: padding: 1px;
22: } 46: border-spacing: 10px;
23: 47: }

Prepared by: Prof. EMILY F. SICAT Page 1


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
48: 76:
49: td.footer1{ 77: ol a:before {
50: text-align: center; 78: content: counter(li);
51: background-color: #FFFFFF; 79: counter-increment: li;
52: font-family: 'Trebuchet MS', 'Lucida Sans 80: position: absolute;
Unicode', 'Lucida Grande', 'Lucida Sans'; 81: left: -2.5em;
53: font-size:14px;} 82: top: 50%;
54: 83: margin-top: -1em;
55: ol { 84: background: #f9dd94;
56: counter-reset: li; 85: height: 2em;
57: list-style: none; 86: width: 2em;
58: padding: 0; 87: line-height: 2em;
59: text-shadow: 0 1px 0 rgba(255,255,255,.5); 88: text-align: center;
60: } 89: font-weight: bold;
61: 90: }
62: ol a { 91:
63: position: relative; 92: ol a:after {
64: display: block; 93: position: absolute;
65: padding: .4em .4em .4em .8em; 94: content: "";
66: margin: .5em 0 .5em 2.5em; 95: border: .5em solid transparent;
67: background: #D3D4DA; 96: left: -1em;
68: color: #444; 97: top: 50%;
69: text-decoration: none; 98: margin-top: -.5em;
70: transition: all .3s ease-out; 99: transition: all .3s ease-out;
71: } 100: }
72: 101:
73: ol a:hover { 102: ol a:hover:after {
74: background: #DCDDE1; 103: left: -.5em;
75: } 104: border-left-color: #f9dd94;
105: }

FILENAME: CONFIG.PHP

1: <?php
2: //define system wide settings
3: define("ORGNAME", 'University of Makati');
4: define("DBHOST", "localhost");
5: define("DBUSER", "root");
6: define("DBPASSWORD", "");
7: define("DBNAME", "dbStudents");
8: ?>

FILENAME: FOOTER.PHP

1: <table class="footer">
2: <tr>
a. <td class="footer1">Copyright <?php echo date("Y"); ?></td>
3: </tr>
4: </table>

Prepared by: Prof. EMILY F. SICAT Page 2


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
5: </body>
6: </html>

FILENAME: HEADER.PHP

1: <html>
2: <head>
3: <title>
4: <?php
5: //print the page title
6: if (defined('TITLE'))
7: {
8: //is the title defined
9: print TITLE;
10: }
11: else
12: {
13: //Title is not defined
14: print "All about the main title of the page...";
15: }
16: ?>
17: </title>
18: <link rel="stylesheet" href="css/styles_css.css">
19: </head>
20: <body>
21: <table class="banner">
22: <tr>
23: <td class="banner1">Welcome to <?php print ORGNAME ?> Students' Database</td>
24: </tr>
25: </table>

FILENAME: DISPLAY_MESSAGE.PHP

1: <?php
2: function display_message($message)
3: {
4: ?>
5: <br><br>
6: <table class="message">
7: <tr>
8: <td class="message1"><?php echo $message ?></td>
9: </tr>

Prepared by: Prof. EMILY F. SICAT Page 3


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
10: </table>
11: <?php
12: }
13: ?>

FILENAME: INDEX.PHP

1: <?php
2: //define the title for this page
3: define("TITLE","Welcome to the Student's Database Main page");
4:
5: //include external files
6: include_once 'config.php';
7: include_once 'templates/header.php';
8: include_once 'functions/display_message.php';
9:
10: //Change the message to display
11: display_message("What do you want to do today?");
12: ?>
13: <ol>
14: <li><a href="add_students_record.php">Add New Student</a></li>
15: <li><a href="view_students_record.php">Display List of Students</a></li>
16: <li><a href="search_students_record.php">Search for a student</a></li>
17: </ol>
18: <?php
19: include_once 'templates/footer.php';
20: ?>

FILENAME: ADD_STUDENTS_RECORD.PHP

1: <?php
2: //FILE LOCATION and NAME: <document root>/Students/add_students_record.php
3:
4: //address error handling
5: ini_set('display_errors',1);
6: error_reporting(E_ALL & ~E_NOTICE);
7:
8: //define TITLE for this page
9: define('TITLE','Add Record');
10:
11: //include external files
12: require ("config.php");

Prepared by: Prof. EMILY F. SICAT Page 4


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
13: include_once('functions/display_message.php');
14: include_once('templates/header.php');
15:
16: //change the display message
17: display_message('Add Records');
18:
19: // Create connection
20: $conn = mysqli_connect(DBHOST, DBUSER, DBPASSWORD, DBNAME);
21:
22: // Check connection
23: if (!$conn) {
24: die("Connection failed: " . mysqli_connect_error());
25: }
26:
27: //The add_button was clicked.
28: if (isset($_POST['add_button']))
29: {
30: //store the inputted value in the text field into variables
31: $studentid = $_POST['studentid'];
32: $fname = $_POST['fname'];
33: $lname = $_POST['lname'];
34: $gender = $_POST['gender'];
35: $email = $_POST['email'];
36: $bday = $_POST['bday'];
37:
38: $query = "INSERT INTO tblstudents(studID, fname, lname, gender, email, birthday)
39: VALUES ('$studentid', '$fname', '$lname', '$gender','$email','$bday')";
40:
41: //execute the query statement
42: $result = mysqli_query($conn, $query);
43:
44: //checked if the execution of query is correct
45: if ($result)
46: {
47: echo "One record is added.<br><br>";
48: echo "<a href=\"index.php\">Back to menu</a>";
49: }
50: else
51: { //error in the execution of the query
52: echo "Error: " . $sql . "<br>" . mysqli_error($conn);
53: }
54: } // end if add button clicked
55: else

Prepared by: Prof. EMILY F. SICAT Page 5


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
56: {
57: ?>
58: <form method = "post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
59: <p>Student ID: <INPUT type="text" name="studentid"> </p>
60: <p>First Name: <INPUT type="text" name="fname"> </p>
61: <p>Last Name: <INPUT type="text" name="lname"> </p>
62: <p>Gender: <INPUT type="text" name="gender"> </p>
63: <p>Email: <INPUT type="text" name="email"> </p>
64: <p>Birthday: <INPUT type="text" name="bday"> </p>
65: <p><input type="submit" name="add_button" value="Add Student"></p>
66: </form>
67: <?php
68: } // end else – ADD BUTTON IS CLICKED
69:
70: //close connection
71: mysqli_close($conn);
72: echo "<a href=\"index.php\">Back to menu</a>";
73:
74: //include the external file footer.php
75: include_once('templates/footer.php');
76: ?>

FILENAME: VIEW_STUDENTS_RECORD.PHP

1: <?php
2: //FILE LOCATION and NAME: <document root>/Students/view_students_record.php
3:
4: //address error handling
5: ini_set('display_errors',1);
6: error_reporting(E_ALL & ~E_NOTICE);
7:
8: //define TITLE for this page
9: define('TITLE','View Records');
10:
11: //include the external files(reuse files)
12: include_once 'config.php';
13: include_once 'templates/header.php';
14: include_once 'functions/display_message.php';
15:
16: //set the displaying message
17: display_message("List of Students");
18:

Prepared by: Prof. EMILY F. SICAT Page 6


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
19: //create connection and access the database
20: $conn = mysqli_connect(DBHOST,DBUSER,DBPASSWORD,DBNAME);
21:
22: // Check connection
23: if (!$conn) {
24: die("Connection failed: " . mysqli_connect_error());
25: }
26:
27: $sql = "SELECT * FROM tblStudents";
28: $result = mysqli_query($conn, $sql);
29:
30: //display each records vertically
31: echo "<h1>Records displayed vertically</h1>";
32: if (mysqli_num_rows($result) > 0) {
33: // output data of each row
34: while($row = mysqli_fetch_assoc($result)) {
35: //date_create() converts the data in the birthday field into a date
36: //Example : "1973-08-02"==>1973-08-02
37: $bday = date_create($row["birthday"]);
38:
39: echo "ID: ".$row["studID"]."<br>";
40: echo "Last Name: ".$row["lname"]."<br>";
41: echo "First Name: ".$row["fname"]."<br>";
42: echo "Email Address: ".$row["email"]."<br>";
43: echo "Birthday : ".$row["birthday"]."<br>";
44: echo "Birthday : ".date_format($bday,"F d, Y")."<br>";
45: echo "Gender: ".$row["gender"]."<br>";
46:
47: echo "<hr>";
48: echo "<br>";
49: }//close of while loop
50: }
51: else {
52: echo "0 results";
53: }
54:
55: echo "<hr>";
56:
57: //or display each records horizontally
58: echo "<h1>Records displayed horizontally</h1>";
59:
60: $result = mysqli_query($conn, $sql);
61: if (mysqli_num_rows($result) > 0) {

Prepared by: Prof. EMILY F. SICAT Page 7


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
62: // output data of each row
63: while($row = mysqli_fetch_assoc($result)) {
64: $bday = date_create($row["birthday"]);
65:
66: echo "ID: " . $row["studID"]. " - Name: " . $row["fname"]. " " . $row["lname"]. " - Email : ".
$row["email"]. " - Birthday : ". $row["birthday"]. "<br>";
67: echo "<br><hr>";
68: echo "<br>";
69: }//close of while loop
70: }
71: ?>
72: <h1>Records displayed in table</h1>
73: <hr>
74: <table>
75: <tr>
76: <th>ID</th>
77: <th>Name</th>
78: <th>Gender</th>
79: <th>Birthday</th>
80: <th>E-mail</th>
81: <th>Action</th>
82: </tr>
83: <?php
84: $result = mysqli_query($conn, $sql);
85:
86: //or each records can be displayed horizontally in a table
87: if (mysqli_num_rows($result) > 0) {
88: // output data of each row
89: while($row = mysqli_fetch_assoc($result)) {
90: $bday = date_create($row["birthday"]);
91:
92: //$fullname = $row["fname"]." ".$row["lname"];
93: //the variable $fullname concats the fname and lname
94: echo "<tr>";
95: echo "<td>" . $row["studID"] . "</td>";
96: //echo "<td>" . $fullname . "</td>"; use this if you use the $fullname variable
97: echo "<td>" . $row["fname"]. " " . $row["lname"] . "</td>";
98: echo "<td>". $row["gender"] . "</td>";
99: echo "<td>". date_format($bday,"F d, Y") . "</td>";
100: echo "<td>". $row["email"] . "</td>";
101: echo "<td>". "<a href=\"delete_students_record.php\">Delete</a><br><a
href=\"edit_students_record.php\">Edit</a>" ."</td>";
102: echo "</tr>";

Prepared by: Prof. EMILY F. SICAT Page 8


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
103: }//close of while loop
104: } else {
105: echo "0 results";
106: }
107: ?>
108: </table>
109: <hr>
110: <?php
111: //close the connection
112: mysqli_close($conn);
113:
114: echo "<a href=\"index.php\">Back to menu</a>";
115:
116: //include the externa file footer.php
117: include_once 'templates/footer.php';
118: ?>

FILENAME: SEARCH_STUDENTS_RECORD.PHP

1: <?php
2: //FILE LOCATION and NAME: <document root>/Students/search_students_record.php
3:
4: //address error handling
5: ini_set('display_errors',1);
6: error_reporting(E_ALL & ~E_NOTICE);
7:
8: //define TITLE for this page
9: define('TITLE','Search Records');
10:
11: //include external files
12: include("config.php");
13: include_once('functions/display_message.php');
14: include_once('templates/header.php');
15:
16: //set the displaying message
17: display_message('Search Records');
18:
19: // Create connection
20: $conn = mysqli_connect(DBHOST, DBUSER, DBPASSWORD, DBNAME);
21:
22: // Check connection
23: if (!$conn) {

Prepared by: Prof. EMILY F. SICAT Page 9


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
24: die("Connection failed: " . mysqli_connect_error());
25: }
26: ?>
27: <br><br>
28: <form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
29: <label>Student ID.</label><input type="text" name="id" />
30: <input type="submit" name="search" value="Search" />
31: </form>
32: <?php
33: //the search button was clicked
34: if (isset($_GET['search'])) {
35: if (isset($_GET['id'])) {
36: $id = (int) $_GET['id'];
37:
38: $sql = "SELECT * FROM tblstudents WHERE studID = " . $id;
39: $result = mysqli_query($conn, $sql);
40:
41: if (mysqli_num_rows($result) > 0) {
42: // output data of each row
43: while ($row = mysqli_fetch_assoc($result)) {
44: //display the found record
45: echo "ID: " . $row["studID"]. " - Name: " . $row["fname"]. " " . $row["lname"]. " - Email : ".
$row["email"]. " - Birthday : ". $row["birthday"]. "<br>";
46: }//close of while($row = mysqli_fetch_assoc($result))
47: } //close of if(mysqli_num_rows($result) > 0 )
48: else {
49: echo "Record with an ID no. ".$_GET['id']." is not in the database.";
50: }
51: } //close of if (isset($_GET[‘id’]))
52: }// close of if (isset($_GET[‘search’]))
53:
54: //close connection
55: mysqli_close($conn);
56: ?>
57: <?php
58: echo "<br>";
59: echo "<a href=\"index.php\">Back to menu</a>";
60:
61: //include the external file footer.php
62: include_once('templates/footer.php');
63: ?>

Prepared by: Prof. EMILY F. SICAT Page 10


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
FILENAME: DELETE_STUDENTS_RECORD.PHP

Code to follow

FILENAME: EDIT_STUDENTS_RECORD.PHP

Code to follow

Prepared by: Prof. EMILY F. SICAT Page 11


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY
SCREENSHOTS OF OUTPUT:

FILENAME: index.php FILENAME: add_students_record.php

FILENAME: view_students_record.php FILENAME: view_students_record.php


(DISPLAY RECORDS VERTICALLY) (DISPLAY RECORDS HORIZONTALLY)

Prepared by: Prof. EMILY F. SICAT Page 12


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY

FILENAME: view_students_record.php FILENAME: search_students_record.php


(DISPLAY RECORDS in a TABLE) (Record found in the database)

FILENAME: search_students_record.php FILENAME: delete_students_record.php


(Record NOT found in the database) (screenshot to follow)

Prepared by: Prof. EMILY F. SICAT Page 13


Faculty, College of Computer Science
COMPROG2 – PHP & MYSQL DATABASE
CONNECTIVITY

FILENAME: edit_students_record.php
(screenshot to follow)

Prepared by: Prof. EMILY F. SICAT Page 14


Faculty, College of Computer Science

You might also like