Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 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. $title = $text = "";
  7. $title_err = $text_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11.     // Validate Title
  12.     $input_title = trim($_POST["titel"]);
  13.     if(empty($input_title)){
  14.         $title_err = "Please enter your title.";
  15.     } elseif(!filter_var($input_title, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
  16.         $title_err = "Please enter a valid title.";
  17.     } else{
  18.         $title = $input_title;
  19.     }
  20.  
  21.     // Validate text
  22.     $input_text = trim($_POST["tekst"]);
  23.     if(empty($input_text)){
  24.         $text_err = "Please enter your text.";
  25.     } else{
  26.         $text = $input_text;
  27.     }
  28.  
  29.  
  30.     // Check input errors before inserting in database
  31.     if(empty($title_err) && empty($text_err)){
  32.         // Prepare an insert statement
  33.         $sql = "INSERT INTO blog (titel, tekst) VALUES (?, ?)";
  34.  
  35.         if($stmt = mysqli_prepare($link, $sql)){
  36.             // Bind variables to the prepared statement as parameters
  37.             mysqli_stmt_bind_param($stmt, "string", $param_title, $param_text);
  38.  
  39.             // Set parameters
  40.             $param_title = $title;
  41.             $param_text = $text;
  42.  
  43.             // Attempt to execute the prepared statement
  44.             if(mysqli_stmt_execute($stmt)){
  45.                 // Records created successfully. Redirect to landing page
  46.                 header("location: index.php");
  47.                 exit();
  48.             } else{
  49.                 echo "Something went wrong. Please try again later.";
  50.             }
  51.         }
  52.  
  53.         // Close statement
  54.         mysqli_stmt_close($stmt);
  55.     }
  56.  
  57.     // Close connection
  58.     mysqli_close($link);
  59. }
  60.     ?>
  61.         <?php
  62.         include 'include/header.php';
  63.         ?>
  64.  
  65.                                 <h2>Blog aanmaken</h2>
  66.                             <p>Vul de gegevens in om de blog aan te maken.</p>
  67.                             <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  68.  
  69.                                 <div class="form-group <?php echo (!empty($title_err)) ? 'has-error' : ''; ?>">
  70.                                     <label>Titel</label>
  71.                                     <input type="text" name="titel" class="form-control" value="<?php echo $title; ?>">
  72.                                     <span class="help-block"><?php echo $title_err;?></span>
  73.                                 </div>
  74.  
  75.                                 <div class="form-group <?php echo (!empty($text_err)) ? 'has-error' : ''; ?>">
  76.                                     <label>Tekst</label>
  77.                                     <textarea name="tekst" ><?php echo $text; ?></textarea>
  78.                                     <span class="help-block"><?php echo $text_err;?></span>
  79.                                 </div>
  80.  
  81.                                 <input type="submit" class="btn btn-primary" value="Submit">
  82.                                 <a href="index.php" class="btn btn-default">Cancel</a>
  83.                             </form>
  84.  
  85.     </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement