Advertisement
Guest User

Roopavathy Baskaran

a guest
Aug 18th, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8" />
  5.  
  6.         <title>Edit user</title>
  7.  
  8.         <link rel="stylesheet" href="css/main.css">
  9.     </head>
  10.     <body>
  11.         <?php
  12.  
  13.             require 'db/connect.php';
  14.  
  15.             $errors  = array();
  16.  
  17.             if (isset($_GET['id'])){
  18.  
  19.                 // Get id from the link
  20.                 $id = $_GET['id'];
  21.  
  22.                 if (empty($id)){
  23.  
  24.                     header ('Location: index.php');
  25.                 } else {
  26.  
  27.                     // Get user information from ID
  28.                     if ($results = $db->query("SELECT * FROM `people` WHERE `id` = '{$id}'")){
  29.  
  30.                         if ($results->num_rows){
  31.  
  32.                             while ($row = $results->fetch_object()){
  33.  
  34.                                 $records = $row;
  35.                             }
  36.  
  37.                             $results->free();
  38.                         }
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             // Edt user
  44.             if (isset($_POST['edit'])){
  45.  
  46.                 // Get informations from fields
  47.                 $first_name = trim($_POST['first_name']);
  48.                 $last_name  = trim($_POST['last_name']);
  49.  
  50.                 // Check fields
  51.                 if (empty($first_name) || empty($last_name)){
  52.  
  53.                     $errors[] = '<br /><font color="red">Fields cannot be empty!</font>';
  54.                 } else {
  55.  
  56.                     // Check length of first_name
  57.                     if (strlen($first_name) < 3 || strlen($first_name) > 15){
  58.  
  59.                         $errors[] = '<br /><font color="red">First name needs to contain minimum 3 and maximum 15 chars!</font>';
  60.                     } else {
  61.  
  62.                         // Check lenght of last_name
  63.                         if (strlen($last_name) < 3 || strlen($last_name) > 20){
  64.  
  65.                             $errors[] = '<br /><font color="red">Last name needs to contain minimum 3 and maximum 20 chars!</font>';
  66.                         } else {
  67.  
  68.                             // Update user
  69.                             $update = $db->prepare("UPDATE `people` SET `first_name` = ?, `last_name` = ?, `created` = NOW() WHERE `id` = '{$id}'");
  70.                             $update->bind_param('ss', $first_name, $last_name);
  71.  
  72.                             if ($update->execute()){
  73.  
  74.                                 header('Location: index.php');
  75.                                 die();
  76.                             } else {
  77.  
  78.                                 $errors[] = '<br /><font color="red">Currently you cannot update users. Admins are trying to fix this issue!</font>';
  79.                             }
  80.                         }
  81.                     }
  82.                 }
  83.  
  84.             }
  85.  
  86.         ?>
  87.         <div class="content">
  88.             <div class="edit_user">
  89.                 <?php
  90.  
  91.                     if (count($records)){
  92.  
  93.                 ?>
  94.                 <h3>Edit users: </h3>
  95.  
  96.                 <form method="post" action="">
  97.                     <table>
  98.                         <tr>
  99.                             <td>First name:</td>
  100.                             <td><input type="text" name="first_name" value="<?php echo $records->first_name; ?>"></td>
  101.                         </tr>
  102.                         <tr>
  103.                             <td>Last name:</td>
  104.                             <td><input type="text" name="last_name" value="<?php echo $records->last_name; ?>"></td>
  105.                         </tr>
  106.                         <tr>
  107.                             <td></td>
  108.                             <td><input type="submit" name="edit" value="Edit"></td>
  109.                         </tr>
  110.                     </table>
  111.                 </form><!-- end form -->
  112.                 <?php
  113.  
  114.                     } else {
  115.  
  116.                         $errors[] = '<br /><font color="red">That user does not exist!</font>';
  117.                     }
  118.  
  119.                     echo implode ($errors);
  120.                 ?>
  121.             </div><!-- edit_user -->
  122.         </div><!-- end content -->
  123.     </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement