You are on page 1of 14

19BPS1126 E2 Kshitij Lariwal

Lab Assignment - 10

1.Create a database

<?php

$servername = "localhost"; $username = "root"; $password = "";

// Create connection
$conn = new mysqli($servername, $username, $password); // Check
connection
if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error); }

// Create database
$sql = "CREATE DATABASE student";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

2.Create a table student with attributes regno, name,gpa and email by setting
regno as primary key.

3.Insert 3 records
4.List the student details whose gpa < 9.
5.Update a record and display the updated tuples.

index.php
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Lab 10 - SQL PHP</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/
bootstrap.min.css"






19BPS1126 E2 Kshitij Lariwal
rel="stylesheet" integrity="sha384-
1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">

</head>
<body>
<div class="container">
<div class="card">
<div class="card-image">
<h2 class="card-heading">
GPA Data Collection
<small>Academics Office</small>
<img src="https://findlogovector.com/wp-

content/uploads/2019/03/vellore-institute-of-technology-vit-logo-
vector.png" alt=""> </h2>

</div>
<form action="index.php" method="post" class="card-form">

<div class="input">
<input type="text" class="input-field"
value="" name="reg_no"
id="reg_no" required />
<label class="input-label">Registration Number</label>

required />

/>

</div>
<div class="input">
<input type="text" class="input-field" value="" name="name"
id="name"

<label class="input-label">Student Name</label> </div>

<div class="input">
<input type="text" class="input-field" name="email" id="email"
required

<label class="input-label">Email Address</label> </div>

<div class="input">
<input type="GPA" class="input-field" name="gpa" id="gpa" required
/> <label class="input-label">GPA</label>

</div>
<center>
<div class="action btn btn-primary">
<button class="">Submit</button>
</div>
<div class=" action btn btn-primary">




19BPS1126 E2 Kshitij Lariwal


<button class="" name="view">View</button>
</div>
</center>
</form>
<div class=" card-info">
<p>Created by 19BPS1126 - Z</a></p>
</div>

</div>
<style>
@import

url('https://fonts.googleapis.com/css2?
family=DM+Sans:wght@400;500;700&display=swap');
*,
*:after,
*:before {
box-sizing: border-box;
}
body {
font-family: "DM Sans", sans-serif;
line-height: 1.5;
background-color: #f1f3fb;
padding: 0 2rem;
}

img {
max-width: 100%;
display: block;
}

input {
appearance: none;
border-radius: 0;
}

.card {
margin: 2rem auto;
display: flex;
flex-direction: column;
width: 100%;
max-width: 425px;
background-color: #FFF;
border-radius: 10px;
box-shadow: 0 10px 20px 0 rgba(#999, .25);
padding: .75rem;
}

.card-image {
border-radius: 8px;
overflow: hidden;
padding-bottom: 65%;

19BPS1126 E2 Kshitij Lariwal


background-repeat: no-repeat;
background-size: 150%;
background-position: 0 5%;
position: relative;
}

.card-heading {
position: absolute;
left: 10%;
top: 15%;
right: 10%;
font-size: 1.75rem;
font-weight: 700;
color: #735400;
line-height: 1.222;
small {
display: block;
font-size: .75em;
font-weight: 400;
margin-top: .25em;
} }

.card-form {
padding: 2rem 1rem 0;
}

.input {
display: flex;
flex-direction: column-reverse;
position: relative;
padding-top: 1.5rem;
}

.input-label {
color: #8597a3;
position: absolute;
top: 1.5rem;
transition: .25s ease;
}

.input-field { border: 0;

z-index: 1;
background-color: transparent; border-bottom: 2px solid #eee;
font: inherit;
font-size: 2.125rem;
padding: 1.25rem 0;

.action {
margin-top: 2rem;
}

19BPS1126 E2 Kshitij Lariwal


.action-button {
font: inherit;
font-size: 1.25rem; padding: 1em;
width: 100%;
font-weight: 500; background-color: #6658d3; border-radius: 6px;
color: #FFF;

border: 0;

.card-info {
padding: 1rem 1rem; text-align: center; font-size: .875rem; color:
#8597a3;

</style>

</body>
</html><?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lab10";
// Create connection
$conn = new mysqli(
$servername,
$username,
$password,
$dbname
);

// Check connection
if ($conn->connect_error) {
die("Connection failed: "
. $conn->connect_error);

//post the form data to the database


if ($_SERVER["REQUEST_METHOD"] == "POST") { $reg_no =
$_POST["reg_no"];
$name = $_POST["name"];
$email = $_POST["email"];

$gpa = $_POST["gpa"];
$sql = "INSERT INTO student (reg_no, name, email, gpa)
VALUES ('$reg_no', '$name', '$email', '$gpa')";

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


//center align the text
echo "<center>";



19BPS1126 E2 Kshitij Lariwal


echo "<h1>New record created successfully</h1>";
echo "</center>";
} else {
echo "<center>";
echo "List Of Students With GPA < 9";
echo "</center>";
}
}

//display the data from the database in a table with border and
padding of 1rem and a background color of #f1f3fb once VIEW button
is clicked

//show the data in a bootstrap table with border and padding of


1rem and a background color of #f1f3fb

if (isset($_POST['view'])) {
$sql = "SELECT * FROM student WHERE gpa < 9 ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<center>";
echo "<table border='1' cellpadding='1'
cellspacing='1'
style='width:50%'>";
echo "<tr>";
echo "<th>Reg No</th>";
echo "<th>Name</th>";
echo "<th>Email</th>";
echo "<th>GPA</th>";
echo "</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["reg_no"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["gpa"] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
} else {
echo "0 results";
} }

$conn->close();
?>

19BPS1126 E2 Kshitij Lariwal


19BPS1126 E2 Kshitij Lariwal
19BPS1126 E2 Kshitij Lariwal

Part B
With the already created database and the table student with attributes
regno, name,gpa and email by setting regno as primary key, perform the
following.

1.Design a form with regno, name, gpa and email as elds. 2.Validate each
eld
3.Get the values from the form and insert the same into the table.

4.Create another form with regno as eld. Get a value and delete the record
with that regno from the table.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Lab 10 - SQL PHP</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/
bootstrap.min.css"

rel="stylesheet" integrity="sha384-
1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">

</head>
<body>
<div class="container">
<div class="card">
<div class="card-image">
<h2 class="card-heading">
GPA Data Collection
<small>Academics Office</small>
<img src="https://findlogovector.com/wp-

content/uploads/2019/03/vellore-institute-of-technology-vit-logo-
vector.png" alt=""> </h2>

</div>
<form action="index.php" method="post" class="card-form">

<div class="input">
<input type="text" class="input-field"
value="" name="reg_no"
id="reg_no" required />
<label class="input-label">Registration Number</label>
fi










fi

fi

19BPS1126 E2 Kshitij Lariwal
</div>

<center>
<div class="action btn btn-primary">
<button class="">Delete</button>
</div>
</center>
</form>
<div class=" card-info">
<p>Created by 19BPS11261089 – KSHITIJ </a></p>
</div>
</div>
<style>
@import
url('https://fonts.googleapis.com/css2?
family=DM+Sans:wght@400;500;700&display=swap');
*,
*:after,
*:before {
box-sizing: border-box;
}
body {
font-family: "DM Sans", sans-serif;
line-height: 1.5;
background-color: #f1f3fb;
padding: 0 2rem;
}

img {
max-width: 100%;
display: block;
}

input {
appearance: none;
border-radius: 0;
}

.card {
margin: 2rem auto;
display: flex;
flex-direction: column;
width: 100%;
max-width: 425px;
background-color: #FFF;
border-radius: 10px;
box-shadow: 0 10px 20px 0 rgba(#999, .25);
padding: .75rem;
}

.card-image {
border-radius: 8px;

19BPS1126 E2 Kshitij Lariwal


overflow: hidden;
padding-bottom: 65%;
background-repeat: no-repeat;
background-size: 150%;
background-position: 0 5%;
position: relative;
}

.card-heading {
position: absolute;
left: 10%;
top: 15%;
right: 10%;
font-size: 1.75rem;
font-weight: 700;
color: #735400;
line-height: 1.222;
small {
display: block;
font-size: .75em;
font-weight: 400;
margin-top: .25em;
}

</style>

.card-form {
padding: 2rem 1rem 0;
}

.input {
display: flex;
flex-direction: column-reverse;
position: relative;
padding-top: 1.5rem;
}

.input-label {
color: #8597a3;
position: absolute;
top: 1.5rem;
transition: .25s ease;
}

.input-field { border: 0;

z-index: 1;
background-color: transparent; border-bottom: 2px solid #eee;
font: inherit;
font-size: 2.125rem;
padding: 1.25rem 0;

19BPS1126 E2 Kshitij Lariwal


}

.action {
margin-top: 2rem;
}

.action-button {
font: inherit;
font-size: 1.25rem; padding: 1em;
width: 100%;
font-weight: 500; background-color: #6658d3; border-radius: 6px;
color: #FFF;

border: 0;

.card-info {
padding: 1rem 1rem; text-align: center; font-size: .875rem; color:
#8597a3;

</body>
</html><?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lab10";
// Create connection
$conn = new mysqli(
$servername,
$username,
$password,
$dbname
);

// Check connection
if ($conn->connect_error) {
die("Connection failed: "
. $conn->connect_error);

//delete the record from the database once the button is


clicked
if (isset($_POST['reg_no'])) {
$reg_no = $_POST['reg_no'];
$sql = "DELETE FROM `student` WHERE `reg_no` = '$reg_no'"; if
($conn->query($sql) === TRUE) {

//center the text


echo "<center>";



19BPS1126 E2 Kshitij Lariwal


echo "Record deleted successfully";
echo "</center>";
} else {
echo "Error deleting record: " . $conn->error;
} }

$conn->close();
?>

19BPS1126 E2 Kshitij Lariwal

You might also like