crud complete
This commit is contained in:
1 parent
a7f793b22a
commit
51ca61242d
4 files changed
+328
No files matched your search
+76
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
// Process delete operation after confirmation
|
||||||
|
if(isset($_POST["id"]) && !empty($_POST["id"])){
|
||||||
|
// Include config file
|
||||||
|
require_once "config.php";
|
||||||
|
|
||||||
|
// Prepare a delete statement
|
||||||
|
$sql = "DELETE FROM employees WHERE id = ?";
|
||||||
|
|
||||||
|
if($stmt = mysqli_prepare($link, $sql)){
|
||||||
|
// Bind variables to the prepared statement as parameters
|
||||||
|
mysqli_stmt_bind_param($stmt, "i", $param_id);
|
||||||
|
|
||||||
|
// Set parameters
|
||||||
|
$param_id = trim($_POST["id"]);
|
||||||
|
|
||||||
|
// Attempt to execute the prepared statement
|
||||||
|
if(mysqli_stmt_execute($stmt)){
|
||||||
|
// Records deleted successfully. Redirect to landing page
|
||||||
|
header("location: index.php");
|
||||||
|
exit();
|
||||||
|
} else{
|
||||||
|
echo "Oops! Something went wrong. Please try again later.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close statement
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
|
|
||||||
|
// Close connection
|
||||||
|
mysqli_close($link);
|
||||||
|
} else{
|
||||||
|
// Check existence of id parameter
|
||||||
|
if(empty(trim($_GET["id"]))){
|
||||||
|
// URL doesn't contain id parameter. Redirect to error page
|
||||||
|
header("location: error.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Delete Record</title>
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||||
|
<style>
|
||||||
|
.wrapper{
|
||||||
|
width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="wrapper">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-12">
|
||||||
|
<h2 class="mt-5 mb-3">Delete Record</h2>
|
||||||
|
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
|
||||||
|
<div class="alert alert-danger">
|
||||||
|
<input type="hidden" name="id" value="<?php echo trim($_GET["id"]); ?>"/>
|
||||||
|
<p>Are you sure you want to delete this employee record?</p>
|
||||||
|
<p>
|
||||||
|
<input type="submit" value="Yes" class="btn btn-danger">
|
||||||
|
<a href="index.php" class="btn btn-secondary">No</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Error</title>
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||||
|
<style>
|
||||||
|
.wrapper{
|
||||||
|
width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="wrapper">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-12">
|
||||||
|
<h2 class="mt-5 mb-3">Invalid Request</h2>
|
||||||
|
<div class="alert alert-danger">Sorry, you've made an invalid request. Please <a href="index.php" class="alert-link">go back</a> and try again.</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -32,3 +32,59 @@ if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
|
|||||||
header("location: error.php");
|
header("location: error.php");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else{
|
||||||
|
echo "Oops! Something went wrong. Please try again later.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close statement
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
|
|
||||||
|
// Close connection
|
||||||
|
mysqli_close($link);
|
||||||
|
} else{
|
||||||
|
// URL doesn't contain id parameter. Redirect to error page
|
||||||
|
header("location: error.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>View Record</title>
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||||
|
<style>
|
||||||
|
.wrapper{
|
||||||
|
width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="wrapper">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-12">
|
||||||
|
<h1 class="mt-5 mb-3">View Record</h1>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Name</label>
|
||||||
|
<p><b><?php echo $row["name"]; ?></b></p>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Address</label>
|
||||||
|
<p><b><?php echo $row["address"]; ?></b></p>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Salary</label>
|
||||||
|
<p><b><?php echo $row["salary"]; ?></b></p>
|
||||||
|
</div>
|
||||||
|
<p><a href="index.php" class="btn btn-primary">Back</a></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+170
@@ -0,0 +1,170 @@
|
|||||||
|
<?php
|
||||||
|
// Include config file
|
||||||
|
require_once "config.php";
|
||||||
|
|
||||||
|
// Define variables and initialize with empty values
|
||||||
|
$name = $address = $salary = "";
|
||||||
|
$name_err = $address_err = $salary_err = "";
|
||||||
|
|
||||||
|
// Processing form data when form is submitted
|
||||||
|
if(isset($_POST["id"]) && !empty($_POST["id"])){
|
||||||
|
// Get hidden input value
|
||||||
|
$id = $_POST["id"];
|
||||||
|
|
||||||
|
// Validate name
|
||||||
|
$input_name = trim($_POST["name"]);
|
||||||
|
if(empty($input_name)){
|
||||||
|
$name_err = "Please enter a name.";
|
||||||
|
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
|
||||||
|
$name_err = "Please enter a valid name.";
|
||||||
|
} else{
|
||||||
|
$name = $input_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate address address
|
||||||
|
$input_address = trim($_POST["address"]);
|
||||||
|
if(empty($input_address)){
|
||||||
|
$address_err = "Please enter an address.";
|
||||||
|
} else{
|
||||||
|
$address = $input_address;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate salary
|
||||||
|
$input_salary = trim($_POST["salary"]);
|
||||||
|
if(empty($input_salary)){
|
||||||
|
$salary_err = "Please enter the salary amount.";
|
||||||
|
} elseif(!ctype_digit($input_salary)){
|
||||||
|
$salary_err = "Please enter a positive integer value.";
|
||||||
|
} else{
|
||||||
|
$salary = $input_salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check input errors before inserting in database
|
||||||
|
if(empty($name_err) && empty($address_err) && empty($salary_err)){
|
||||||
|
// Prepare an update statement
|
||||||
|
$sql = "UPDATE employees SET name=?, address=?, salary=? WHERE id=?";
|
||||||
|
|
||||||
|
if($stmt = mysqli_prepare($link, $sql)){
|
||||||
|
// Bind variables to the prepared statement as parameters
|
||||||
|
mysqli_stmt_bind_param($stmt, "sssi", $param_name, $param_address, $param_salary, $param_id);
|
||||||
|
|
||||||
|
// Set parameters
|
||||||
|
$param_name = $name;
|
||||||
|
$param_address = $address;
|
||||||
|
$param_salary = $salary;
|
||||||
|
$param_id = $id;
|
||||||
|
|
||||||
|
// Attempt to execute the prepared statement
|
||||||
|
if(mysqli_stmt_execute($stmt)){
|
||||||
|
// Records updated successfully. Redirect to landing page
|
||||||
|
header("location: index.php");
|
||||||
|
exit();
|
||||||
|
} else{
|
||||||
|
echo "Oops! Something went wrong. Please try again later.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close statement
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close connection
|
||||||
|
mysqli_close($link);
|
||||||
|
} else{
|
||||||
|
// Check existence of id parameter before processing further
|
||||||
|
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
|
||||||
|
// Get URL parameter
|
||||||
|
$id = trim($_GET["id"]);
|
||||||
|
|
||||||
|
// Prepare a select statement
|
||||||
|
$sql = "SELECT * FROM employees WHERE id = ?";
|
||||||
|
if($stmt = mysqli_prepare($link, $sql)){
|
||||||
|
// Bind variables to the prepared statement as parameters
|
||||||
|
mysqli_stmt_bind_param($stmt, "i", $param_id);
|
||||||
|
|
||||||
|
// Set parameters
|
||||||
|
$param_id = $id;
|
||||||
|
|
||||||
|
// Attempt to execute the prepared statement
|
||||||
|
if(mysqli_stmt_execute($stmt)){
|
||||||
|
$result = mysqli_stmt_get_result($stmt);
|
||||||
|
|
||||||
|
if(mysqli_num_rows($result) == 1){
|
||||||
|
/* Fetch result row as an associative array. Since the result set
|
||||||
|
contains only one row, we don't need to use while loop */
|
||||||
|
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
||||||
|
|
||||||
|
// Retrieve individual field value
|
||||||
|
$name = $row["name"];
|
||||||
|
$address = $row["address"];
|
||||||
|
$salary = $row["salary"];
|
||||||
|
} else{
|
||||||
|
// URL doesn't contain valid id. Redirect to error page
|
||||||
|
header("location: error.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else{
|
||||||
|
echo "Oops! Something went wrong. Please try again later.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close statement
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
|
|
||||||
|
// Close connection
|
||||||
|
mysqli_close($link);
|
||||||
|
} else{
|
||||||
|
// URL doesn't contain id parameter. Redirect to error page
|
||||||
|
header("location: error.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Update Record</title>
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||||
|
<style>
|
||||||
|
.wrapper{
|
||||||
|
width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="wrapper">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-12">
|
||||||
|
<h2 class="mt-5">Update Record</h2>
|
||||||
|
<p>Please edit the input values and submit to update the employee record.</p>
|
||||||
|
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Name</label>
|
||||||
|
<input type="text" name="name" class="form-control <?php echo (!empty($name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $name; ?>">
|
||||||
|
<span class="invalid-feedback"><?php echo $name_err;?></span>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Address</label>
|
||||||
|
<textarea name="address" class="form-control <?php echo (!empty($address_err)) ? 'is-invalid' : ''; ?>"><?php echo $address; ?></textarea>
|
||||||
|
<span class="invalid-feedback"><?php echo $address_err;?></span>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Salary</label>
|
||||||
|
<input type="text" name="salary" class="form-control <?php echo (!empty($salary_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $salary; ?>">
|
||||||
|
<span class="invalid-feedback"><?php echo $salary_err;?></span>
|
||||||
|
</div>
|
||||||
|
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
|
||||||
|
<input type="submit" class="btn btn-primary" value="Submit">
|
||||||
|
<a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in new issue
Block a user