Advertisement
irfanamir

read.php

Nov 12th, 2020
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2. // Check existence of id parameter before processing further
  3. if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
  4.     // Include config file
  5.     require_once "config.php";
  6.    
  7.     // Prepare a select statement
  8.     $sql = "SELECT * 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($_GET["id"]);
  16.        
  17.         // Attempt to execute the prepared statement
  18.         if(mysqli_stmt_execute($stmt)){
  19.             $result = mysqli_stmt_get_result($stmt);
  20.    
  21.             if(mysqli_num_rows($result) == 1){
  22.                 /* Fetch result row as an associative array. Since the result set
  23.                 contains only one row, we don't need to use while loop */
  24.                 $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  25.                
  26.                 // Retrieve individual field value
  27.                 $name = $row["name"];
  28.                 $address = $row["address"];
  29.                 $salary = $row["salary"];
  30.             } else{
  31.                 // URL doesn't contain valid id parameter. Redirect to error page
  32.                 header("location: error.php");
  33.                 exit();
  34.             }
  35.            
  36.         } else{
  37.             echo "Oops! Something went wrong. Please try again later.";
  38.         }
  39.     }
  40.      
  41.     // Close statement
  42.     mysqli_stmt_close($stmt);
  43.    
  44.     // Close connection
  45.     mysqli_close($link);
  46. } else{
  47.     // URL doesn't contain id parameter. Redirect to error page
  48.     header("location: error.php");
  49.     exit();
  50. }
  51. ?>
  52. <!DOCTYPE html>
  53. <html lang="en">
  54. <head>
  55.     <meta charset="UTF-8">
  56.     <title>View Record</title>
  57.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  58.     <style type="text/css">
  59.         .wrapper{
  60.             width: 500px;
  61.             margin: 0 auto;
  62.         }
  63.     </style>
  64. </head>
  65. <body>
  66.     <div class="wrapper">
  67.         <div class="container-fluid">
  68.             <div class="row">
  69.                 <div class="col-md-12">
  70.                     <div class="page-header">
  71.                         <h1>View Record</h1>
  72.                     </div>
  73.                     <div class="form-group">
  74.                         <label>Name</label>
  75.                         <p class="form-control-static"><?php echo $row["name"]; ?></p>
  76.                     </div>
  77.                     <div class="form-group">
  78.                         <label>Address</label>
  79.                         <p class="form-control-static"><?php echo $row["address"]; ?></p>
  80.                     </div>
  81.                     <div class="form-group">
  82.                         <label>Salary</label>
  83.                         <p class="form-control-static"><?php echo $row["salary"]; ?></p>
  84.                     </div>
  85.                     <p><a href="index.php" class="btn btn-primary">Back</a></p>
  86.                 </div>
  87.             </div>        
  88.         </div>
  89.     </div>
  90. </body>
  91. </html>
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement