The following is my code to insert the personal and address details of an employee in SQL database:
<?php
include_once 'config.php';
$name = $_POST['name'];
$age = $_POST['age'];
$salary = $_POST['salary'];
$street = $_POST['street'];
$area = $_POST['area'];
$city = $_POST['city'];
$nation = $_POST['nation'];
//Inserts the personal details.
$personal_details = "INSERT INTO `tbl_personal_details`(`pk_emp_id`, `name`, `age`, `salary`)"
. " VALUES (NULL,'$name', '$age', $salary')";
if ($conn->query($personal_details)) {
$fk_emp_id = mysqli_insert_id($conn);
//Inserts the address details.
$address_details = "INSERT INTO `tbl_address_details`(`pk_add_id`,`fk_emp_id`, `street`, `area`, `city`, `nation`)"
. " VALUES (NULL,'$fk_emp_id','$street','$area', '$city', $nation')";
if ($conn->query($address_details)) {
echo "Employee successflly added.";
} else {
//Rolls back the personal details if error in inserting the address details.
$personal_details->rollback();
echo "Error in inserting the address values. " . $conn->error;
}
} else {
echo "Error in inserting the personal details." . $conn->error;
}
?>
Is this the right way of inserting the data of an employee into the database. And if so, is my rollback function correct to handle the error while inserting the address details of the employee.
Any help would be great. Thanks in advance.