0% found this document useful (0 votes)
9 views7 pages

Partc Iot

The document provides a PHP and JavaScript code example for managing employee information in a database. It includes scripts for connecting to a MySQL database, inserting employee data with validation, displaying employee records, updating employee details, deleting records based on salary, and sending work anniversary wishes using AJAX. The code demonstrates form validation, database operations, and dynamic content updates on a web page.

Uploaded by

OneMan'sVoice
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)
9 views7 pages

Partc Iot

The document provides a PHP and JavaScript code example for managing employee information in a database. It includes scripts for connecting to a MySQL database, inserting employee data with validation, displaying employee records, updating employee details, deleting records based on salary, and sending work anniversary wishes using AJAX. The code demonstrates form validation, database operations, and dynamic content updates on a web page.

Uploaded by

OneMan'sVoice
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/ 7

20191CSE0318

MANOHARA S

20191CSE0318

7CSE06

PARTC

1)

a) PHP Script to establish a connection with the database:

<?php

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "Startech";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

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

echo "Connected successfully";

?>

B). Insert 6 Employee information into the EMPLOYEE table received through web page.
Validate the Emp Mail ID and Mobile number(only 10 digits) using Javascript

<html>
20191CSE0318

<head>

<script>

function validateForm() {

var empID = document.forms["employeeForm"]["empID"].value;

var empName = document.forms["employeeForm"]["empName"].value;

var empMail = document.forms["employeeForm"]["empMail"].value;

var address = document.forms["employeeForm"]["address"].value;

var mobile = document.forms["employeeForm"]["mobile"].value;

var salary = document.forms["employeeForm"]["salary"].value;

if (empID == "") {

alert("Emp ID must be filled out");

return false;

if (empName == "") {

alert("Emp Name must be filled out");

return false;

if (empMail == "") {

alert("Emp Mail ID must be filled out");

return false;

if (address == "") {

alert("Address must be filled out");

return false;
20191CSE0318

if (mobile == "" || mobile.length != 10) {

alert("Mobile Number must be filled out and should be 10 digits");

return false;

if (salary == "") {

alert("Salary must be filled out");

return false;

//Insert data into the database

<?php

$sql = "INSERT INTO EMPLOYEE (EmpID, EmpName, EmpMailID, Address, MobileNumber, Salary)

VALUES ('$_POST[empID]', '$_POST[empName]', '$_POST[empMail]', '$_POST[address]',


'$_POST[mobile]', '$_POST[salary]')";

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

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

?>

</script>

</head>

<body>
20191CSE0318

<form name="employeeForm" onsubmit="return validateForm()" method="post">

Emp ID: <input type="text" name="empID"><br>

Emp Name: <input type="text" name="empName"><br>

Emp Mail ID: <input type="text" name="empMail"><br>

Address: < textarea name="address"></textarea><br>

Mobile Number: <input type="text" name="mobile"><br>

Salary: <input type="text" name="salary"><br>

<input type="submit" value="Submit">

</form>

</body>

</html>

C). Display records of all employees against table columns (Emp Name, Address, Mobile
Number, Salary fields) to the browser page by retrieving from the EMPLOYEE table.

<?php

$sql = "SELECT EmpName, Address, MobileNumber, Salary FROM EMPLOYEE";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "Emp Name: " . $row["EmpName"]. " - Address: " . $row["Address"]. " - Mobile Number: " .
$row["MobileNumber"]. " - Salary: " . $row["Salary"]. "<br>";

}
20191CSE0318

} else {

echo "0 results";

$conn->close();

?>

D). Make a provision to UPDATE Mobile number and Salary of any one existing employee
through webpage.

E). Demonstrate how to DELETE any Employee details having salary more than 50000.

<?php

$sql = "DELETE FROM EMPLOYEE WHERE Salary > 50000";

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

echo "Record(s) deleted successfully";

} else {

echo "Error deleting record: " . $conn->error;

$conn->close();

?>

F) Write a Ajax program to wish a Employee on work anniversary using XMLHttpRequest


object.

<html>

<body>

<form>
20191CSE0318

Emp ID: <input type="text" id="empID"><br>

<input type="button" value="Wish Employee" onclick="sendRequest()">

</form>

<p id="demo"></p>

<script>

function sendRequest()

var xhr = new XMLHttpRequest();

var empID = document.getElementById("empID").value;

xhr.open("GET", "wish.php?empID=" + empID, true);

xhr.send();

xhr.onreadystatechange = function() {

if (xhr.readyState == 4 && xhr.status == 200) {

document.getElementById("demo").innerHTML = xhr.responseText;

};

</script>

</body>

</html>
20191CSE0318

Wish.php:

<?php

$empID = $_GET['empID'];

//Retrieve employee details from the database

$sql = "SELECT EmpName, WorkAnniversary FROM EMPLOYEE WHERE EmpID='$empID'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

$row = $result->fetch_assoc();

$empName = $row["EmpName"];

$workAnniversary = $row["WorkAnniversary"];

echo "Happy Work Anniversary, $empName! Today, $workAnniversary, marks another year of your hard
work and dedication. Keep up the good work!";

} else {

echo "Invalid Employee ID";

$conn->close();

?>

You might also like