Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.02 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. $titleUpdate = $textUpdate = "";
  7. $titleUpdate_err = $textUpdate_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 title
  15.     if($input_titleUpdate = trim(isset($_POST["titel"]))){
  16.         /*$input_titleUpdate = (!empty($_POST[""])) ? trim($_POST["titel"]) : "";*/
  17.         if (isset($input_titleUpdate)) {
  18.             $titleUpdate_err = "Voer uw titel in.";
  19.         } elseif (!filter_var($input_titleUpdate, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zA-Z\s]+$/")))) {
  20.             $titleUpdate_err = "Uw titel is niet valiede";
  21.         } else {
  22.             $name = $input_titleUpdate;
  23.         }
  24.     }
  25.  
  26.  
  27.     // Validate text
  28.     $input_textUpdate = trim($_POST["tekst"]);
  29.     if(empty($input_textUpdate)){
  30.         $textUpdate_err = "Uw bericht.";
  31.     } else{
  32.         $textUpdate = $input_textUpdate;
  33.     }
  34.  
  35.  
  36.     // Check input errors before inserting in database
  37.     if(empty($titleUpdate_err) && empty($textUpdate_err)){
  38.         // Prepare an update statement
  39.         $sql = "UPDATE blog SET titel=?, tekst=? WHERE id=?";
  40.  
  41.         if($stmt = mysqli_prepare($link, $sql)){
  42.             // Bind variables to the prepared statement as parameters
  43.             mysqli_stmt_bind_param($stmt, "ssi", $param_titleUpdate, $param_textUpdate, $param_id);
  44.  
  45.             // Set parameters
  46.             $param_titleUpdate = $titleUpdate;
  47.             $param_textUpdate = $textUpdate;
  48.             $param_id = $id;
  49.  
  50.             // Attempt to execute the prepared statement
  51.             if(mysqli_stmt_execute($stmt)){
  52.                 // Records updated successfully. Redirect to landing page
  53.                 header("location: ../blog.php");
  54.                 exit();
  55.             } else{
  56.                 echo "Iets ging fout probeer het later opnieuw.";
  57.             }
  58.         }
  59.  
  60.         // Close statement
  61.         mysqli_stmt_close($stmt);
  62.     }
  63.  
  64.     // Close connection
  65.     mysqli_close($link);
  66. } else{
  67.     // Check existence of id parameter before processing further
  68.     if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
  69.         // Get URL parameter
  70.         $id =  trim($_GET["id"]);
  71.  
  72.         // Prepare a select statement
  73.         $sql = "SELECT titel, tekst FROM blog WHERE id = ?";
  74.         if($stmt = mysqli_prepare($link, $sql)){
  75.             // Bind variables to the prepared statement as parameters
  76.             mysqli_stmt_bind_param($stmt, "i", $param_id);
  77.  
  78.             // Set parameters
  79.             $param_id = $id;
  80.  
  81.             // Attempt to execute the prepared statement
  82.             if(mysqli_stmt_execute($stmt)){
  83.                 $result = mysqli_stmt_get_result($stmt);
  84.  
  85.                 if(mysqli_num_rows($result) == 1){
  86.                     /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
  87.                     $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  88.  
  89.                     // Retrieve individual field value
  90.                     $titelUpdate = $row["titel"];
  91.                     $textUpdate = $row["tekst"];
  92.                 } else{
  93.                     // URL doesn't contain valid id. Redirect to error page
  94.                     header("location: error.php");
  95.                     exit();
  96.                 }
  97.  
  98.             } else{
  99.                 echo "Oops! Iets ging daar fout, probeer het later opnieuw.";
  100.             }
  101.         }
  102.  
  103.         // Close statement
  104.         /* mysqli_stmt_close($stmt); */
  105.  
  106.         // Close connection
  107.         mysqli_close($link);
  108.     }  else{
  109.         // URL doesn't contain id parameter. Redirect to error page
  110.         header("location: error.php");
  111.         exit();
  112.     }
  113. }
  114.  
  115. include '../include/header.php'
  116.  
  117. ?>
  118.  
  119. <body>
  120.  
  121.                     <h2>Update Record</h2>
  122.  
  123.                 <p>Please edit the input values and submit to update the record.</p>
  124.                 <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
  125.                     <div <?php echo (!empty($titleUpdate_err)) ? 'has-error' : ''; ?>">
  126.                         <label>Titel</label>
  127.                         <input type="text" name="titelUpdate" value="<?php echo $titleUpdate; ?>">
  128.                         <span><?php echo $titleUpdate_err;?></span>
  129.                     </div>
  130.                     <div <?php echo (!empty($textUpdate_err)) ? 'has-error' : ''; ?>">
  131.                         <label>Tekst</label>
  132.                         <textarea name="tekst"><?php echo $textUpdate; ?></textarea>
  133.                         <span><?php echo $textUpdate_err;?></span>
  134.                     </div>
  135.  
  136.                     <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  137.                     <input type="submit" value="Submit">
  138.                     <a href="../blog.php">Cancel</a>
  139.                 </form>
  140.  
  141. </body>
  142. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement