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

Lab 12

1. The document describes creating a database called "University" with a table called "Employee" to store information on 10 employees. It includes PHP code to connect to the database and perform CRUD operations. 2. An HTML file is created to take an employee ID from the user and make an AJAX request to a PHP script to retrieve and display the corresponding employee details from the database in a table. 3. The PHP code connects to the database, queries the "Employee" table for the given ID, and outputs the results in HTML table format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views8 pages

Lab 12

1. The document describes creating a database called "University" with a table called "Employee" to store information on 10 employees. It includes PHP code to connect to the database and perform CRUD operations. 2. An HTML file is created to take an employee ID from the user and make an AJAX request to a PHP script to retrieve and display the corresponding employee details from the database in a table. 3. The PHP code connects to the database, queries the "Employee" table for the given ID, and outputs the results in HTML table format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Web technologies-12

Name: Aditya Ram


Reg no:20BCE7102
1) Implement the following activities using PHP, AJAX and MySQL:
a) Create a database with the name “University”
b) In the “University” database, create a table with the name “Employee” and with the attributes
“Id”, “Firstname”, “Lastname”, “Department”, “Designation”
c) Insert information of around 10 employees into the “Employee” table

Create an HTML document which contain a text field for entering the “Id” of an employee. When the
user enters the “Id” of employee in the text field then make an AJAX request to a PHP script which
retrieves the details of concerned employee from the database and display the details in the web
page in tabular format.

<?php
$servername="127.0.0.1";
$username="root";
$password='';
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS University";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully\n";
} else {
    echo "Error creating database: " . mysqli_error($conn) . "\n";
}

// Select database
mysqli_select_db($conn, "University");

// Create table
$sql = "CREATE TABLE IF NOT EXISTS Employee (
    Id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Firstname VARCHAR(30) NOT NULL,
    Lastname VARCHAR(30) NOT NULL,
    Department VARCHAR(30) NOT NULL,
    Designation VARCHAR(30) NOT NULL
)";
if (mysqli_query($conn, $sql)) {
    echo "Table created successfully\n";
} else {
    echo "Error creating table: " . mysqli_error($conn) . "\n";
}

// Insert data
$sql = "INSERT INTO Employee (Firstname, Lastname, Department,
Designation)
    VALUES
    ('John', 'Doe', 'IT', 'Manager'),
    ('Jane', 'Doe', 'HR', 'Coordinator'),
    ('Bob', 'Smith', 'Finance', 'Analyst'),
    ('Alice', 'Johnson', 'Marketing', 'Manager'),
    ('David', 'Lee', 'IT', 'Developer'),
    ('Amy', 'Wang', 'Marketing', 'Coordinator'),
    ('Tom', 'Wilson', 'Finance', 'Manager'),
    ('Sarah', 'Liu', 'IT', 'Analyst'),
    ('Steve', 'Garcia', 'HR', 'Manager'),
    ('Cathy', 'Chen', 'IT', 'Coordinator')";
if (isset($_POST['id'])) {
  $id = $_POST['id'];

  $sql = "SELECT * FROM Employee WHERE Id = $id";


  $result = mysqli_query($conn, $sql);

  if (mysqli_num_rows($result) > 0) {
    echo "<table>";
    echo "<tr><th>ID</th><th>First Name</th><th>Last
Name</th><th>Department</th><th>Designation</th></tr>";
    while ($row = mysqli_fetch_assoc($result)) {
      echo "<tr><td>" . $row['Id'] . "</td><td>" . $row['Firstname']
. "</td><td>" . $row['Lastname'] . "</td><td>" .
$row['Department'] . "</td><td>" . $row['Designation'] .
"</td></tr>";
    }
    echo "</table>";
  } else {
    echo "No employee found with ID = $id";
  }
}

mysqli_close($conn);
?>

Emp1.html:
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Employee Details</title>
  <script>
    function getEmployeeDetails() {
      var id = document.getElementById("employee-id").value;

      var xhr = new XMLHttpRequest();


      xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("employee-details").innerHTML =
this.responseText;
        }
      };
      xhr.open("POST", "get_employee_details.php", true);
      xhr.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
      xhr.send("id=" + id);
    }
  </script>
  <style>
    table {
      border-collapse: collapse;
      width: 100%;
    }
    th, td {
      text-align: left;
      padding: 8px;
    }
    tr:nth-child(even) {
      background-color: #f2f2f2;
    }
  </style>
</head>
<body>
  <h1>Employee Details</h1>
  <form>
    <label for="employee-id">Enter employee ID:</label>
    <input type="text" id="employee-id" name="employee-id">
    <button type="button" onclick="getEmployeeDetails()">Get
details</button>
  </form>
  <div id="employee-details"></div>

</body>
</html>

Output:
2) Write a PHP script to access the data from the following “books.xml” file and display the data in
the following format using Expat parser

Category: category of book


Title: title of book
Author: author of book
Year: year of book
Price: price of book

“books.xml”
<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">
    <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>

Q2as12.php

<?php
$xml = new XMLReader();
$xml->open('books.xml');

while ($xml->read()) {
  if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == 'book')
{
    $category = $xml->getAttribute('category');
    $title = '';
    $author = '';
    $year = '';
    $price = '';
    while ($xml->read()) {
      if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'title') {
        $title = $xml->readString();
      }
      if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'author') {
        $author = $xml->readString();
      }
      if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'year') {
        $year = $xml->readString();
      }
      if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'price') {
        $price = $xml->readString();
      }
      if ($xml->nodeType == XMLReader::END_ELEMENT && $xml->name ==
'book') {
        echo "Category: $category <br>";
        echo "Title: $title <br>";
        echo "Author: $author <br>";
        echo "Year: $year <br>";
        echo "Price: $price <br><br>";
        break;
      }
    }
  }
}
$xml->close();
?>

Output:
Books.xml:

You might also like