0% found this document useful (0 votes)
88 views8 pages

Laboratory Manual 3 PHP Database Programming: Visual Studio Code Manual

Download as pdf or txt
0% found this document useful (0 votes)
88 views8 pages

Laboratory Manual 3 PHP Database Programming: Visual Studio Code Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Visual Studio Code Manual 2021

Laboratory Manual 3
PHP Database Programming
Database – Introduction
A database management system (DBMS) defines, creates and maintains a database. DBMS also allows
controlled access to data in the database.

 Networked RDBMS
– Oracle
• For large mission critical systems runs on Linux
– MS SQL Server
• Used in small to medium sized systems run in Windows server
– DB2
• For large mission critical systems runs on IBM OS
– MySQL/MariaDB
• Open source, runs on major operating system, commonly used in web application
– Apache Derby/Java DB
• New java based relation database, can be embedded in application
– …
 Embedded RDBMS
– Apache Derby/Java DB
– MS Access
– SQLite
– Compact version of MS SQL, Oracle, …

Connecting PHP Application to MySQL/MariaDB Database System


MySQL, the most popular Open Source SQL database management system, is developed, distributed, and
supported by Oracle Corporation. The MySQL Web site (http://www.mysql.com/) provides the latest
information about MySQL software. You can also download MySQL along with PHP, and other as third part
package such as XAMPP (https://www.apachefriends.org/download.html), WAMP
(https://www.wampserver.com/en/#download-wrapper) , etc.

i. Design and Create Database system for your application


1. Study the requirement and design database, UML Class diagram

Computer Science Department 1


Visual Studio Code Manual 2021

2. Create database and database objects, use appropriate tool to design and develop database for your
application; such as
a. PhpMyAdmin
b. VS Code extension tool, such as ApexSql, SQL tool, etc.
c. MySQL Workbench
d. MySQL DOS Command
e. etc.

ii. Connecting to MySQL from PhpMyadmin


1. start XAMPP
2. Login to PhpMyAdmin (http://localhost/phpmyadmin)
3. Create database “registrar” with SQL code or in design
CREATE DATABASE `registrar`
4. Create table college (id, name, phone) with SQL code or in design

CREATE TABLE `college`


(`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) DEFAULT NULL,
`phone` VARCHAR(20) DEFAULT NULL,
PRIMARY KEY(`id`)
);

Computer Science Department 2


Visual Studio Code Manual 2021

iii. Create Project in VS Code


1. Create folder ‘registrar’ in C:/xampp/htdocs/ (where you installed xampp)
2. Open the folder in VS Code
3. Create folder “Core”, “views” and “content’ folder
4. Create file “College.php” and “DbConnection.php” in core folder
5. Create folder “college’ in views folder
6. Create file ‘register.php’ and ‘registerform.php’ in views/coellege folder, your project structure look as
follow

7. Open DbConnection.php file and create a class “DbConnection” and define your database setting and
connection class

Computer Science Department 3


Visual Studio Code Manual 2021

<?php
class DbConnection
{
private $host = "localhost";
private $db = "registrar";
private $username = "chalewt";
private $password = "laravel";
public function connect()
{
try {
$pdo = new PDO("mysql:host=" . $this->host . ";dbname:" . $this-
>db, $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
die("ERROR: Could not connect. " . $e->getMessage());
}
}
}

iv. Database – Insert operation


1. Open College.php file and create a class College and implement ‘register’ method

<?php
require_once 'DbConnection.php';
class College
{
private $id;
private $name;
private $phone;

//Setter & Getter


public function setId($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}

Computer Science Department 4


Visual Studio Code Manual 2021

public function getName()


{
return $this->name;
}
public function setPhone($phone)
{
$this->phone = $phone;
}
public function getPhone()
{
return $this->phone;
}
//Methods
public function register($college)
{
$sql = "Insert into registrar.college(name, phone) values(:name, :phone)";
$dbConn = new DbConnection();
$conn = $dbConn->connect();
$query = $conn->prepare($sql);
$query->bindValue(':name', $college->getName());
$query->bindValue(':phone', $college->getPhone());
$query->execute();
$is_success = $conn->lastInsertId();
return $is_success;
}
}

2. Open ‘registerform.php’ file and design the College Registration Form.


You may use bootstrap to make your design responsive
Download bootstrap and place in content folder
Download jquery and place in content/js folder

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>College Register</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-
fit=no">
<link rel="stylesheet" href="../../content/bootstrap/css/bootstrap.min.css">

</head>

Computer Science Department 5


Visual Studio Code Manual 2021

<body>
<script src="../../content/js/jquery-3.5.1.min.js"></script>
<script src="../../content/bootstrap/js/bootstrap.min.js"></script>
<div class="container">
<div class="row col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-heading txtt-center">
<h3>College Registration Form</h3>
</div>
<div class="panel-body">
<form action="register.php" method="POST">
<div class="form-group">
<label for="txtName">College</label>
<input class="form-
control" type="text" id="txtName" name="txtName" placeholder="College">
</div>
<div class="form-group">
<label for="txtPhone">Phone</label>
<input type="text" class="form-
control" id="txtPhone" name="txtPhone" placeholder="Phone">
</div>
<div class="form-group">
<input type="submit" value="Save" name="submit" class="btn
btn-primary" />
</div>
</form>
</div>
</div>
</div>
</div>
</body>

</html>

3. Open ‘register.php’ and write the action code for registration form.

<?php
require '../../core/College.php';
$txtName = $_POST['txtName'];
$txtPhone = $_POST['txtPhone'];
$college = new College();
$college->setName($txtName);
$college->setPhone($txtPhone);
$is_success = $college->register($college);
if ($is_success == true) {

Computer Science Department 6


Visual Studio Code Manual 2021

echo "Record saved successfully.";


} else {
echo "Sorry, Something went wrong. Try again.";
}

4. Run and test your application


Browse to http://localhost/registrar/views/college/registerform.php
fill the “College Registration Form” and Click ‘Save’

Your application should respond with success.

You can also check the record in your database via PhpMyAdmin

v. Assignment (10pt) – Complete the rest CRUD operation


1. Work in pair
2. Clone the startup project(registrar) from the gitlab repositoryand delete the local ‘.git’ folder from the
project directory and create/enable a new local repository
3. Create a gitlab project/repository called “assignment-1” with one of your partner account and add other
as a developer member
4. Publish the startup project to the project you created in #3

Computer Science Department 7


Visual Studio Code Manual 2021

5. Implement all the remaining methods of the “College” class, i.e


a. +get_all():college
b. +update(int):bool
c. +delete(int):bool
d. +get_by_id(int):College
6. Design appropriate UI for each requirements/methods, such as
a. index.php to list all colleges
b. deleteform.php to enable search and delete with confirmation
c. updateform.php to enable search and update with confirmation
d. searchform.php to enable accept id and search college (Bonus: search with name/name initial)
7. Write appropriate action code for each UI action button
8. When you do some change, commit as early as possible

Computer Science Department 8

You might also like