irfanamir

update.php

Nov 12th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.57 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once "config.php";
  4.  
  5. // Define variables and initialize with empty values
  6. $name = $address = $salary = "";
  7. $name_err = $address_err = $salary_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if(isset($_POST["id"]) && !empty($_POST["id"])){
  11.     // Get hidden input value
  12.     $id = $_POST["id"];
  13.    
  14.     // Validate name
  15.     $input_name = trim($_POST["name"]);
  16.     if(empty($input_name)){
  17.         $name_err = "Please enter a name.";
  18.     } elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
  19.         $name_err = "Please enter a valid name.";
  20.     } else{
  21.         $name = $input_name;
  22.     }
  23.    
  24.     // Validate address address
  25.     $input_address = trim($_POST["address"]);
  26.     if(empty($input_address)){
  27.         $address_err = "Please enter an address.";    
  28.     } else{
  29.         $address = $input_address;
  30.     }
  31.    
  32.     // Validate salary
  33.     $input_salary = trim($_POST["salary"]);
  34.     if(empty($input_salary)){
  35.         $salary_err = "Please enter the salary amount.";    
  36.     } elseif(!ctype_digit($input_salary)){
  37.         $salary_err = "Please enter a positive integer value.";
  38.     } else{
  39.         $salary = $input_salary;
  40.     }
  41.    
  42.     // Check input errors before inserting in database
  43.     if(empty($name_err) && empty($address_err) && empty($salary_err)){
  44.         // Prepare an update statement
  45.         $sql = "UPDATE employees SET name=?, address=?, salary=? WHERE id=?";
  46.          
  47.         if($stmt = mysqli_prepare($link, $sql)){
  48.             // Bind variables to the prepared statement as parameters
  49.             mysqli_stmt_bind_param($stmt, "sssi", $param_name, $param_address, $param_salary, $param_id);
  50.            
  51.             // Set parameters
  52.             $param_name = $name;
  53.             $param_address = $address;
  54.             $param_salary = $salary;
  55.             $param_id = $id;
  56.            
  57.             // Attempt to execute the prepared statement
  58.             if(mysqli_stmt_execute($stmt)){
  59.                 // Records updated successfully. Redirect to landing page
  60.                 header("location: index.php");
  61.                 exit();
  62.             } else{
  63.                 echo "Something went wrong. Please try again later.";
  64.             }
  65.         }
  66.          
  67.         // Close statement
  68.         mysqli_stmt_close($stmt);
  69.     }
  70.    
  71.     // Close connection
  72.     mysqli_close($link);
  73. } else{
  74.     // Check existence of id parameter before processing further
  75.     if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
  76.         // Get URL parameter
  77.         $id =  trim($_GET["id"]);
  78.        
  79.         // Prepare a select statement
  80.         $sql = "SELECT * FROM employees WHERE id = ?";
  81.         if($stmt = mysqli_prepare($link, $sql)){
  82.             // Bind variables to the prepared statement as parameters
  83.             mysqli_stmt_bind_param($stmt, "i", $param_id);
  84.            
  85.             // Set parameters
  86.             $param_id = $id;
  87.            
  88.             // Attempt to execute the prepared statement
  89.             if(mysqli_stmt_execute($stmt)){
  90.                 $result = mysqli_stmt_get_result($stmt);
  91.    
  92.                 if(mysqli_num_rows($result) == 1){
  93.                     /* Fetch result row as an associative array. Since the result set
  94.                     contains only one row, we don't need to use while loop */
  95.                     $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  96.                    
  97.                     // Retrieve individual field value
  98.                     $name = $row["name"];
  99.                     $address = $row["address"];
  100.                     $salary = $row["salary"];
  101.                 } else{
  102.                     // URL doesn't contain valid id. Redirect to error page
  103.                     header("location: error.php");
  104.                     exit();
  105.                 }
  106.                
  107.             } else{
  108.                 echo "Oops! Something went wrong. Please try again later.";
  109.             }
  110.         }
  111.        
  112.         // Close statement
  113.         mysqli_stmt_close($stmt);
  114.        
  115.         // Close connection
  116.         mysqli_close($link);
  117.     }  else{
  118.         // URL doesn't contain id parameter. Redirect to error page
  119.         header("location: error.php");
  120.         exit();
  121.     }
  122. }
  123. ?>
  124.  
  125. <!DOCTYPE html>
  126. <html lang="en">
  127. <head>
  128.     <meta charset="UTF-8">
  129.     <title>Update Record</title>
  130.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  131.     <style type="text/css">
  132.         .wrapper{
  133.             width: 500px;
  134.             margin: 0 auto;
  135.         }
  136.     </style>
  137. </head>
  138. <body>
  139.     <div class="wrapper">
  140.         <div class="container-fluid">
  141.             <div class="row">
  142.                 <div class="col-md-12">
  143.                     <div class="page-header">
  144.                         <h2>Update Record</h2>
  145.                     </div>
  146.                     <p>Please edit the input values and submit to update the record.</p>
  147.                     <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
  148.                         <div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
  149.                             <label>Name</label>
  150.                             <input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
  151.                             <span class="help-block"><?php echo $name_err;?></span>
  152.                         </div>
  153.                         <div class="form-group <?php echo (!empty($address_err)) ? 'has-error' : ''; ?>">
  154.                             <label>Address</label>
  155.                             <textarea name="address" class="form-control"><?php echo $address; ?></textarea>
  156.                             <span class="help-block"><?php echo $address_err;?></span>
  157.                         </div>
  158.                         <div class="form-group <?php echo (!empty($salary_err)) ? 'has-error' : ''; ?>">
  159.                             <label>Salary</label>
  160.                             <input type="text" name="salary" class="form-control" value="<?php echo $salary; ?>">
  161.                             <span class="help-block"><?php echo $salary_err;?></span>
  162.                         </div>
  163.                         <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  164.                         <input type="submit" class="btn btn-primary" value="Submit">
  165.                         <a href="index.php" class="btn btn-default">Cancel</a>
  166.                     </form>
  167.                 </div>
  168.             </div>        
  169.         </div>
  170.     </div>
  171. </body>
  172. </html>
Add Comment
Please, Sign In to add comment