COMPROG3 - Practice Exercise - PHP - MySQL Database Connectivity
COMPROG3 - Practice Exercise - PHP - MySQL Database Connectivity
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
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
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>
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>
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");
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:
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) {
Code to follow
FILENAME: EDIT_STUDENTS_RECORD.PHP
Code to follow
FILENAME: edit_students_record.php
(screenshot to follow)