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
- $titel = $text = "";
- $titel_err = $text_err = "";
- // Processing form data when form is submitted
- if(isset($_POST["id"]) && !empty($_POST["id"])){
- // Get hidden input value
- $id = $_POST["id"];
- // Validate titel
- $input_titel = trim($_POST["titel"]);
- if(empty($input_titel)){
- $titel_err = "Voer uw titel in.";
- } elseif(!filter_var($input_titel, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
- $titel_err = "Voer een geldige titel in.";
- } else{
- $titel = $input_titel;
- }
- // Validate text address
- $input_text = trim($_POST["text"]);
- if(empty($input_text)){
- $text_err = "Please enter an text.";
- } else{
- $text = $input_text;
- }
- // Validate salary
- $input_salary = trim($_POST["salary"]);
- if(empty($input_salary)){
- $salary_err = "Please enter the salary amount.";
- } elseif(!ctype_digit($input_salary)){
- $salary_err = "Please enter a positive integer value.";
- } else{
- $salary = $input_salary;
- }
- // Check input errors before inserting in database
- if(empty($title_err) && empty($text_err) && empty($salary_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, "ss", $param_name, $param_text);
- // Set parameters
- $param_titel = $titel;
- $param_text = $text;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- // Records updated successfully. Redirect to landing page
- header("location: index.php");
- exit();
- } else{
- echo "Something went wrong. Please try again later.";
- }
- }
- // 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 * FROM employees 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
- $titel = $row["name"];
- $text = $row["text"];
- $salary = $row["salary"];
- } else{
- // URL doesn't contain valid id. Redirect to error page
- header("location: error.php");
- exit();
- }
- } else{
- echo "Oops! Something went wrong. Please try again later.";
- }
- }
- // 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';
- ?>
- <div class="wrapper">
- <div class="container-fluid">
- <div class="row">
- <div class="col-md-12">
- <div class="page-header">
- <h2>Update Record</h2>
- </div>
- <p>Please edit the input values and submit to update the record.</p>
- <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
- <div class="form-group <?php echo (!empty($titel_err)) ? 'has-error' : ''; ?>">
- <label>Name</label>
- <input type="text" name="name" class="form-control" value="<?php echo $titel; ?>">
- <span class="help-block"><?php echo $titel_err;?></span>
- </div>
- <div class="form-group <?php echo (!empty($text_err)) ? 'has-error' : ''; ?>">
- <label>Address</label>
- <textarea name="address" class="form-control"><?php echo $text; ?></textarea>
- <span class="help-block"><?php echo $text_err;?></span>
- </div>
- <input type="hidden" name="id" value="<?php echo $id; ?>"/>
- <input type="submit" class="btn btn-primary" value="Submit">
- <a href="index.php" class="btn btn-default">Cancel</a>
- </form>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement