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

Emp

This PHP code connects to a MySQL database, inserts employee data submitted from a form into a database table, and outputs the inserted data. It connects to the database, checks for form submission, inserts the data, retrieves the last inserted record, and outputs it to the page.

Uploaded by

Om Kawate
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 views

Emp

This PHP code connects to a MySQL database, inserts employee data submitted from a form into a database table, and outputs the inserted data. It connects to the database, checks for form submission, inserts the data, retrieves the last inserted record, and outputs it to the page.

Uploaded by

Om Kawate
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/ 3

<?

php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password",
"company_database");

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

// Check if form is submitted


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$position = $_POST["position"];
$salary = $_POST["salary"];

// Insert data into the database


$sql = "INSERT INTO employees (name, position, salary) VALUES ('$name',
'$position', $salary)";

if (mysqli_query($conn, $sql)) {
// Retrieve the last inserted record
$last_id = mysqli_insert_id($conn);
$sql = "SELECT * FROM employees WHERE id=$last_id";
$result = mysqli_query($conn, $sql);

// Check if there are any results


if (mysqli_num_rows($result) > 0) {
// Output data of the last inserted row
echo "<h2>New Employee Added:</h2>";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Position</th><th>Salary</th></tr>";
$row = mysqli_fetch_assoc($result);
echo
"<tr><td>".$row["name"]."</td><td>".$row["position"]."</td><td>".$row["salary"]."</td
></tr>";
echo "</table>";
} else {
echo "No records found";
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}

// Close connection
mysqli_close($conn);
?>

<!DOCTYPE html>
<html>
<head>
<title>Add Employee</title>
</head>
<body>
<h2>Add Employee</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name"><br><br>
Position: <input type="text" name="position"><br><br>
Salary: <input type="text" name="salary"><br><br>
<input type="submit" value="Add">
</form>
</body>
</html>

You might also like