Advertisement
Alkhammash

Untitled

Dec 19th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2. /*
  3.  EDIT.PHP
  4.  Allows user to edit specific entry in database
  5. */
  6.  
  7.  // creates the edit record form
  8.  // since this form is used multiple times in this file, I have made it a function that is easily reusable
  9.  function renderForm($id, $username, $foldername, $location, $caption, $name, $error)
  10.  {
  11.  ?>
  12.  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  13.  <html>
  14.  <head>
  15.   <meta charset="utf-8">
  16.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  17.     <meta name="viewport" content="width=device-width, initial-scale=1">
  18.  <title>Admin User Fodler Delete Record</title>
  19.  </head>
  20.  <body>
  21.  <?php
  22.  // if there are any errors, display them
  23.  if ($error != '')
  24.  {
  25.  echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
  26.  }
  27.  ?>
  28.  
  29.  <form action="" method="post">
  30.  <input type="hidden" name="userfolderid" value="<?php echo $id; ?>"/>
  31.  <div>
  32.  <p><strong>ID:</strong> <?php echo $id; ?></p>
  33.  <strong>UserName: *</strong> <input type="text" name="username" value="<?php echo $username; ?>"/><br/>
  34.  <strong>FolderName: *</strong> <input type="text" name="foldername" value="<?php echo $foldername; ?>"/><br/>
  35.  <strong>Location: *</strong> <input type="text" name="location" value="<?php echo $location; ?>"/><br/>
  36.  <strong>Caption: *</strong> <input type="text" name="caption" value="<?php echo $caption; ?>"/><br/>
  37.  <strong>File Name: *</strong> <input type="text" name="name" value="<?php echo $name; ?>"/><br/>
  38.  <p>* Required</p>
  39.  
  40.  <input type="submit" name="submit" value="Delete">
  41.  </div>
  42.  </form>
  43.  </body>
  44.  </html>
  45.  <?php
  46.  }
  47.  
  48.  
  49.  
  50.  // connect to the database
  51.  include('includes/connection.php');
  52.  
  53.  // check if the form has been submitted. If it has, process the form and save it to the database
  54.  if (isset($_POST['submit']))
  55.  {
  56.  // confirm that the 'id' value is a valid integer before getting the form data
  57.  if (is_numeric($_POST['userfolderid']))
  58.  {
  59.  // get form data, making sure it is valid
  60.  $id = $_POST['userfolderid'];
  61.  $username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
  62.  $foldername = mysql_real_escape_string(htmlspecialchars($_POST['foldername']));
  63.  $location = mysql_real_escape_string(htmlspecialchars($_POST['location']));
  64.  $caption = mysql_real_escape_string(htmlspecialchars($_POST['caption']));
  65.  $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
  66.  
  67.  
  68.  // check that firstname/lastname fields are both filled in
  69.  if ($foldername == '' || $username == '')
  70.  {
  71.  // generate error message
  72.  $error = 'ERROR: Please fill in all required fields!';
  73.  
  74.  //error, display form
  75.  renderForm($id, $username, $foldername, $location, $caption, $name, $error);
  76.  }
  77.  else
  78.  {
  79.  // save the data to the database
  80.  mysql_query("DELETE FROM userfolders WHERE userfolderid='$id'")
  81.  or die(mysql_error());
  82.  
  83.  // once saved, redirect back to the view page
  84.  header("Location: admin-user-folder-details.php");
  85.  }
  86.  }
  87.  else
  88.  {
  89.  // if the 'id' isn't valid, display an error
  90.  echo 'Error!';
  91.  }
  92.  }
  93.  else
  94.  // if the form hasn't been submitted, get the data from the db and display the form
  95.  {
  96.  
  97.  // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
  98.  if (isset($_GET['userfolderid']) && is_numeric($_GET['userfolderid']) && $_GET['userfolderid'] > 0)
  99.  {
  100.  // query db
  101.  $id = $_GET['userfolderid'];
  102.  $result = mysql_query("SELECT * FROM userfolders WHERE userfolderid=$id")
  103.  or die(mysql_error());
  104.  $row = mysql_fetch_array($result);
  105.  
  106.  // check that the 'id' matches up with a row in the databse
  107.  if($row)
  108.  {
  109.  
  110.  // get data from db
  111.  $username = $row['username'];
  112.  $foldername = $row['foldername'];
  113.  $location = $row['location'];
  114.  $caption = $row['caption'];
  115.  $name = $row['name'];
  116.  // show form
  117.  renderForm($id, $username, $foldername, $location, $caption, $name, '');
  118.  }
  119.  else
  120.  // if no match, display result
  121.  {
  122.  echo "No results!";
  123.  }
  124.  }
  125.  else
  126.  // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
  127.  {
  128.  echo 'Error!';
  129.  }
  130.  }
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement