irfanamir

delete.php

Nov 12th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. <?php
  2. // Process delete operation after confirmation
  3. if(isset($_POST["id"]) && !empty($_POST["id"])){
  4.     // Include config file
  5.     require_once "config.php";
  6.    
  7.     // Prepare a delete statement
  8.     $sql = "DELETE FROM employees WHERE id = ?";
  9.    
  10.     if($stmt = mysqli_prepare($link, $sql)){
  11.         // Bind variables to the prepared statement as parameters
  12.         mysqli_stmt_bind_param($stmt, "i", $param_id);
  13.        
  14.         // Set parameters
  15.         $param_id = trim($_POST["id"]);
  16.        
  17.         // Attempt to execute the prepared statement
  18.         if(mysqli_stmt_execute($stmt)){
  19.             // Records deleted successfully. Redirect to landing page
  20.             header("location: index.php");
  21.             exit();
  22.         } else{
  23.             echo "Oops! Something went wrong. Please try again later.";
  24.         }
  25.     }
  26.      
  27.     // Close statement
  28.     mysqli_stmt_close($stmt);
  29.    
  30.     // Close connection
  31.     mysqli_close($link);
  32. } else{
  33.     // Check existence of id parameter
  34.     if(empty(trim($_GET["id"]))){
  35.         // URL doesn't contain id parameter. Redirect to error page
  36.         header("location: error.php");
  37.         exit();
  38.     }
  39. }
  40. ?>
  41. <!DOCTYPE html>
  42. <html lang="en">
  43. <head>
  44.     <meta charset="UTF-8">
  45.     <title>View Record</title>
  46.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  47.     <style type="text/css">
  48.         .wrapper{
  49.             width: 500px;
  50.             margin: 0 auto;
  51.         }
  52.     </style>
  53. </head>
  54. <body>
  55.     <div class="wrapper">
  56.         <div class="container-fluid">
  57.             <div class="row">
  58.                 <div class="col-md-12">
  59.                     <div class="page-header">
  60.                         <h1>Delete Record</h1>
  61.                     </div>
  62.                     <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  63.                         <div class="alert alert-danger fade in">
  64.                             <input type="hidden" name="id" value="<?php echo trim($_GET["id"]); ?>"/>
  65.                             <p>Are you sure you want to delete this record?</p><br>
  66.                             <p>
  67.                                 <input type="submit" value="Yes" class="btn btn-danger">
  68.                                 <a href="index.php" class="btn btn-default">No</a>
  69.                             </p>
  70.                         </div>
  71.                     </form>
  72.                 </div>
  73.             </div>        
  74.         </div>
  75.     </div>
  76. </body>
  77. </html>
Add Comment
Please, Sign In to add comment