0% found this document useful (0 votes)
23 views

Lab 5

This document discusses performing CRUD operations in PHP and MySQL. It provides code examples to create, read, update, and delete data from a database table. It also discusses creating additional tables to practice CRUD operations on different data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lab 5

This document discusses performing CRUD operations in PHP and MySQL. It provides code examples to create, read, update, and delete data from a database table. It also discusses creating additional tables to practice CRUD operations on different data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB 5: CRUD Operations in MySQL with PHP

(Total Points: 100)


Objec ve:
Learn how to implement Create, Read, Update, and Delete (CRUD) opera ons using PHP and MySQL.

Materials:
XAMPP or similar server so ware with PHP and MySQL.
Text editor or IDE for wri ng PHP code.
A MySQL database and table. (Use LabDB and Students table from previous lab ac vi es.)

Procedure:
A. Create (Insert Data) (15 points)
1. Write PHP Script to Insert Data
2. Create a new PHP file named create_data.php.
3. Use the following code to insert new data into the Students table.
Example Code for create_data.php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "LabDB";

// Create connec on
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connec on
if ($conn->connect_error) {
die("Connec on failed: " . $conn->connect_error);
}

// SQL to insert data


$sql = "INSERT INTO Students (firstname, lastname, age, email, course)
VALUES ('John', 'Doe', 20, '[email protected]', 'Computer Science')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

B. Read (Fetch Data) (15 points)

1. Write PHP Script to Fetch Data:


2. Create a new PHP file named read_data.php.
3. Use the following code to fetch and display data from the Students table.

Example Code for read_data.php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "LabDB";

// Create connec on
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connec on
if ($conn->connect_error) {
die("Connec on failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname, age, email, course FROM Students";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. " - Age: " .
$row["age"]. " - Email: " . $row["email"]. " - Course: " . $row["course"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

C. Update (Modify Data) (15 points)


1. Write PHP Script to Update Data:
2. Create a new PHP file named update_data.php.
3. Use the following code to update a record in the Students table.

Example Code for update_data.php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "LabDB";

// Create connec on
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connec on
if ($conn->connect_error) {
die("Connec on failed: " . $conn->connect_error);
}

$sql = "UPDATE Students SET age=21 WHERE firstname='John' AND lastname='Doe'";

if ($conn->query($sql) === TRUE) {


echo "Record updated successfully";
} else {
echo "Error upda ng record: " . $conn->error;
}

$conn->close();
?>

D. Delete (Remove Data) (15 points)


1. Write PHP Script to Delete Data:
2. Create a new PHP file named delete_data.php.
3. Use the following code to delete a record from the Students table.

Example Code for delete_data.php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "LabDB";

// Create connec on
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connec on
if ($conn->connect_error) {
die("Connec on failed: " . $conn->connect_error);
}

$sql = "DELETE FROM Students WHERE firstname='John' AND lastname='Doe'";

if ($conn->query($sql) === TRUE) {


echo "Record deleted successfully";
} else {
echo "Error dele ng record: " . $conn->error;
}

$conn->close();
?>

E. Use a different database when performing the CRUD Opera ons


1. Books Table (20 points)

$sql = "CREATE TABLE Books (


book_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
tle VARCHAR(100) NOT NULL,
author VARCHAR(100),
publish_year INT(4),
genre VARCHAR(50)
)";

2. Cars Table (20 points)


$sql = "CREATE TABLE Cars (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
make VARCHAR(50),
model VARCHAR(50),
year INT(4),
color VARCHAR(30)
)";

Expected Output:
Students show the output when doing CRUD opera ons.

You might also like