Advertisement
Guest User

Untitled

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