Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.43 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once "../include/config.php";
  4.  
  5. // Define variables and initialize with empty values
  6. $titel = $text = "";
  7. $titel_err = $text_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 titel
  15.     $input_titel = trim($_POST["titel"]);
  16.     if(empty($input_titel)){
  17.         $titel_err = "Voer uw titel in.";
  18.     } elseif(!filter_var($input_titel, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
  19.         $titel_err = "Voer een geldige titel in.";
  20.     } else{
  21.         $titel = $input_titel;
  22.     }
  23.  
  24.     // Validate text address
  25.     $input_text = trim($_POST["text"]);
  26.     if(empty($input_text)){
  27.         $text_err = "Please enter an text.";
  28.     } else{
  29.         $text = $input_text;
  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($title_err) && empty($text_err) && empty($salary_err)){
  44.         // Prepare an update statement
  45.         $sql = "UPDATE blog SET titel=?, tekst=? 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, "ss", $param_name, $param_text);
  50.  
  51.             // Set parameters
  52.             $param_titel = $titel;
  53.             $param_text = $text;
  54.  
  55.             // Attempt to execute the prepared statement
  56.             if(mysqli_stmt_execute($stmt)){
  57.                 // Records updated successfully. Redirect to landing page
  58.                 header("location: index.php");
  59.                 exit();
  60.             } else{
  61.                 echo "Something went wrong. Please try again later.";
  62.             }
  63.         }
  64.  
  65.         // Close statement
  66.         mysqli_stmt_close($stmt);
  67.     }
  68.  
  69.     // Close connection
  70.     mysqli_close($link);
  71. } else{
  72.     // Check existence of id parameter before processing further
  73.     if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
  74.         // Get URL parameter
  75.         $id =  trim($_GET["id"]);
  76.  
  77.         // Prepare a select statement
  78.         $sql = "SELECT * FROM employees WHERE id = ?";
  79.         if($stmt = mysqli_prepare($link, $sql)){
  80.             // Bind variables to the prepared statement as parameters
  81.             mysqli_stmt_bind_param($stmt, "i", $param_id);
  82.  
  83.             // Set parameters
  84.             $param_id = $id;
  85.  
  86.             // Attempt to execute the prepared statement
  87.             if(mysqli_stmt_execute($stmt)){
  88.                 $result = mysqli_stmt_get_result($stmt);
  89.  
  90.                 if(mysqli_num_rows($result) == 1){
  91.                     /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
  92.                     $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  93.  
  94.                     // Retrieve individual field value
  95.                     $titel = $row["name"];
  96.                     $text = $row["text"];
  97.                     $salary = $row["salary"];
  98.                 } else{
  99.                     // URL doesn't contain valid id. Redirect to error page
  100.                     header("location: error.php");
  101.                     exit();
  102.                 }
  103.  
  104.             } else{
  105.                 echo "Oops! Something went wrong. Please try again later.";
  106.             }
  107.         }
  108.  
  109.         // Close statement
  110.         mysqli_stmt_close($stmt);
  111.  
  112.         // Close connection
  113.         mysqli_close($link);
  114.     }  else{
  115.         // URL doesn't contain id parameter. Redirect to error page
  116.         header("location: error.php");
  117.         exit();
  118.     }
  119. }
  120.  
  121.     include '../include/header.php';
  122.  
  123. ?>
  124.  
  125. <div class="wrapper">
  126.     <div class="container-fluid">
  127.         <div class="row">
  128.             <div class="col-md-12">
  129.                 <div class="page-header">
  130.                     <h2>Update Record</h2>
  131.                 </div>
  132.                 <p>Please edit the input values and submit to update the record.</p>
  133.                 <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
  134.                     <div class="form-group <?php echo (!empty($titel_err)) ? 'has-error' : ''; ?>">
  135.                         <label>Name</label>
  136.                         <input type="text" name="name" class="form-control" value="<?php echo $titel; ?>">
  137.                         <span class="help-block"><?php echo $titel_err;?></span>
  138.                     </div>
  139.                     <div class="form-group <?php echo (!empty($text_err)) ? 'has-error' : ''; ?>">
  140.                         <label>Address</label>
  141.                         <textarea name="address" class="form-control"><?php echo $text; ?></textarea>
  142.                         <span class="help-block"><?php echo $text_err;?></span>
  143.                     </div>
  144.  
  145.                     <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  146.                     <input type="submit" class="btn btn-primary" value="Submit">
  147.                     <a href="index.php" class="btn btn-default">Cancel</a>
  148.                 </form>
  149.             </div>
  150.         </div>
  151.     </div>
  152. </div>
  153. </body>
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement