Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- require_once "../include/config.php";
- // Define variables and initialize with empty values
- $titleUpdate = $textUpdate = "";
- $titleUpdate_err = $textUpdate_err = "";
- // Processing form data when form is submitted
- if(isset($_POST["id"]) && !empty($_POST["id"])){
- // Get hidden input value
- $id = $_POST["id"];
- $input_titleUpdate = trim($_POST["titel"]);
- if(empty($input_titleUpdate)){
- $titleUpdate_err = "Tiep uw titel in..";
- } elseif(!filter_var($input_titleUpdate, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
- $titleUpdate_err = "Kies een valiede titel.";
- } else{
- $titleUpdate = $input_titleUpdate;
- }
- // Validate text
- $input_textUpdate = trim($_POST["tekst"]);
- if(empty($input_textUpdate)){
- $textUpdate_err = "Uw bericht.";
- } else{
- $textUpdate = $input_textUpdate;
- }
- // Check input errors before inserting in database
- if(empty($titleUpdate_err) && empty($textUpdate_err)){
- // Prepare an update statement
- $sql = "UPDATE blog SET titel=?, tekst=? WHERE id=?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "ssi", $param_titleUpdate, $param_textUpdate, $param_id);
- // Set parameters
- $param_titleUpdate = $titleUpdate;
- $param_textUpdate = $textUpdate;
- $param_id = $id;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- // Records updated successfully. Redirect to landing page
- header("location: ../blog.php");
- exit();
- } else{
- echo "Iets ging fout probeer het later opnieuw.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Close connection
- mysqli_close($link);
- } else{
- // Check existence of id parameter before processing further
- if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
- // Get URL parameter
- $id = trim($_GET["id"]);
- // Prepare a select statement
- $sql = "SELECT titel, tekst FROM blog WHERE id = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "i", $param_id);
- // Set parameters
- $param_id = $id;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- $result = mysqli_stmt_get_result($stmt);
- if(mysqli_num_rows($result) == 1){
- /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
- $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
- // Retrieve individual field value
- $titlelUpdate = $row["titel"];
- $textUpdate = $row["tekst"];
- } else{
- // URL doesn't contain valid id. Redirect to error page
- header("location: error.php");
- exit();
- }
- } else{
- echo "Oops! Iets ging daar fout, probeer het later opnieuw.";
- }
- }
- // Close statement
- /* mysqli_stmt_close($stmt); */
- // Close connection
- mysqli_close($link);
- } else{
- // URL doesn't contain id parameter. Redirect to error page
- header("location: error.php");
- exit();
- }
- }
- include '../include/header.php'
- ?>
- <section id="viewEditDeleteCreate">
- <div class="viewEditDeleteCreateContainer">
- <h2>Update je Blog</h2>
- <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
- <div <?php echo (!empty($titleUpdate_err)) ? 'has-error' : ''; ?>">
- <label>Titel</label>
- <input type="text" name="titel" value="<?php echo $titleUpdate; ?>">
- <span><?php echo $titleUpdate_err;?></span>
- </div>
- <div <?php echo (!empty($textUpdate_err)) ? 'has-error' : ''; ?>">
- <label>Tekst</label>
- <textarea name="tekst"><?php echo $textUpdate; ?></textarea>
- <span><?php echo $textUpdate_err;?></span>
- </div>
- <input type="hidden" name="id" value="<?php echo $id; ?>"/>
- <input type="submit" value="Submit">
- <a href="../blog.php">Cancel</a>
- </form>
- </div>
- </section>
- <?php
- include '../include/footer.php';
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement