Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

20-04-2023 Web Technologies Lab L33+L34 CSE 4004

Lab -11:-

1) Write PHP script to perform the following activities:


(a). Create a database with the name “Company”
(b). In the “Company” database, create a table with the name “Employee” and with the
attributes “EmpNo”, “Firstname”, “Lastname”, “Age”, “Gender”, “Salary”
(c). Insert information of around 10 employees into the “Employee” table

Create an HTML document which contain a text field for entering the firstname of an employee and
a button with the label “Get Details”. Write a PHP script to display the details of concerned
employees in tabular format when the user enters the firstname in the text field and click on the
button.

Database.php

<?php
// Connect to MySQL server
$servername = "localhost";
$username = "CSELAB";
$password = "123";
$conn = mysqli_connect($servername, $username, $password);

// Create the "Company" database


$sql = "CREATE DATABASE Company";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}

// Select the "Company" database


mysqli_select_db($conn, "Company");

// Create the "Employee" table


$sql = "CREATE TABLE Employee (
  EmpNo INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  Firstname VARCHAR(30) NOT NULL,
  Lastname VARCHAR(30) NOT NULL,
  Age INT(3) NOT NULL,
  Gender ENUM('Male', 'Female') NOT NULL,
  Salary FLOAT(8, 2) NOT NULL
)";
if (mysqli_query($conn, $sql)) {
    echo "Table Employee created successfully";
} else {
    echo "Error creating table: " . mysqli_error($conn);

Upendra Sai Chillimuntha | 20BEV7001


20-04-2023 Web Technologies Lab L33+L34 CSE 4004

// Insert employee information


$sql = "INSERT INTO Employee (Firstname, Lastname, Age, Gender, Salary)
VALUES
('John', 'Doe', 30, 'Male', 50000),
('Jane', 'Doe', 25, 'Female', 45000),
('Mark', 'Smith', 40, 'Male', 75000),
('Anna', 'Johnson', 35, 'Female', 65000),
('William', 'Brown', 28, 'Male', 55000),
('Emily', 'Davis', 27, 'Female', 50000),
('Michael', 'Garcia', 32, 'Male', 60000),
('Jessica', 'Wilson', 29, 'Female', 55000),
('David', 'Lee', 45, 'Male', 80000),
('Amanda', 'Miller', 31, 'Female', 60000)";
if (mysqli_query($conn, $sql)) {
    echo "Employee information inserted successfully";
} else {
    echo "Error inserting data: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

form.html

<!DOCTYPE html>
<html>
<head>
  <title>Employee Details</title>
</head>
<body>
  <h1>Enter Firstname to get Employee Details</h1>
  <form action="get_employee_details.php" method="post">
    Firstname: <input type="text" name="firstname"><br>
    <input type="submit" value="Get Details">
  </form>
</body>
</html>

Upendra Sai Chillimuntha | 20BEV7001


20-04-2023 Web Technologies Lab L33+L34 CSE 4004

get_employee_details.php

<?php
// Connect to MySQL server
$servername = "localhost";
$username = "CSELAB";
$password = "123";

$conn = mysqli_connect($servername, $username, $password, "company");

// Retrieve employee details


$firstname = $_POST["firstname"];
$sql = "SELECT * FROM Employee WHERE Firstname='$firstname'";
$result = mysqli_query($conn, $sql);
if (!$result) {
    die("Query failed: " . mysqli_error($conn));

// Display employee details in tabular format


if (mysqli_num_rows($result) > 0) {
    echo "<table border='1'>
    <tr>
      <th>EmpNo</th>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Age</th>
      <th>Gender</th>
      <th>Salary</th>
    </tr>";
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>
      <td>" . $row["EmpNo"] . "</td>
      <td>" . $row["Firstname"] . "</td>
      <td>" . $row["Lastname"] . "</td>
      <td>" . $row["Age"] . "</td>
      <td>" . $row["Gender"] . "</td>
      <td>" . $row["Salary"] . "</td>
    </tr>";
    }
    echo "</table>";
} else {
    echo "No employee found with the given firstname.";
}

mysqli_close($conn);

Upendra Sai Chillimuntha | 20BEV7001


20-04-2023 Web Technologies Lab L33+L34 CSE 4004

On clicking Get Details

2) Write a PHP script to access the data from the following “books.xml” file and display the data in
tabular format

Category Title Author Year Price

books.xml

<?xml version="1.0" encoding="UTF-8"?>


<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">

Upendra Sai Chillimuntha | 20BEV7001


20-04-2023 Web Technologies Lab L33+L34 CSE 4004

    <title lang="en">Harry Potter</title>


    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
</bookstore>

main.php

<?php
// Load XML file
$xml = simplexml_load_file("books.xml");

// Display data in tabular format


echo "<table border='1'>
  <tr>
    <th>Category</th>
    <th>Title</th>
    <th>Author</th>
    <th>Year</th>
    <th>Price</th>
  </tr>";
foreach ($xml->book as $book) {
  echo "<tr>
    <td>".$book['category']."</td>
    <td>".$book->title."</td>
    <td>".$book->author."</td>
    <td>".$book->year."</td>
    <td>".$book->price."</td>
  </tr>";
}
echo "</table>";
?>

Upendra Sai Chillimuntha | 20BEV7001


20-04-2023 Web Technologies Lab L33+L34 CSE 4004

Upendra Sai Chillimuntha | 20BEV7001

You might also like