Advertisement
Eline_VDB

Untitled

Jun 9th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <link   href="css/bootstrap.min.css" rel="stylesheet">
  6.     <script src="js/bootstrap.min.js"></script>
  7. </head>
  8.  
  9. <?php
  10.     require 'database.php';
  11.  
  12.     $id = null;
  13.     if ( !empty($_GET['id'])) {
  14.         $id = $_REQUEST['id'];
  15.     }
  16.  
  17.     if ( null==$id ) {
  18.         header("Location: client.php");
  19.     }
  20.  
  21.     if ( !empty($_POST)) {
  22.         // keep track validation errors
  23.         $firstnameError = null;
  24.         $lastnameError = null;
  25.  
  26.  
  27.         // keep track post values
  28.         $firstname = $_POST['firstname'];
  29.         $lastname = $_POST['lastname'];
  30.  
  31.  
  32.         // validate input
  33.         $valid = true;
  34.         if (empty($firstname)) {
  35.             $nameError = 'Please enter a first name';
  36.             $valid = false;
  37.         }
  38.  
  39.         if (empty($lastname)) {
  40.             $lastnameError = 'Please enter a last name';
  41.             $valid = false;
  42.         }
  43.  
  44.  
  45.         // update data
  46.         if ($valid) {
  47.             $pdo = Database::connect();
  48.             $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  49.             $sql = "UPDATE tl_client  set first_name = ?, last_name = ?, WHERE id = ?";
  50.             $q = $pdo->prepare($sql);
  51.             $q->execute(array($firstname,$lastname,$id));
  52.             Database::disconnect();
  53.             header("Location: client.php");
  54.         }
  55.     } else {
  56.         $pdo = Database::connect();
  57.         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  58.         $sql = "SELECT * FROM tl_client where id = ?";
  59.         $q = $pdo->prepare($sql);
  60.         $q->execute(array($id));
  61.         $data = $q->fetch(PDO::FETCH_ASSOC);
  62.         $firstname = $data['firstname'];
  63.         $lastname = $data['lastname'];
  64.         Database::disconnect();
  65.     }
  66. ?>
  67.  
  68.  
  69. <body>
  70.     <div class="container">
  71.  
  72.         <div class="span10 offset1">
  73.               <div class="row">
  74.                   <h3>Update a Client</h3>
  75.               </div>
  76.  
  77.         <form class="form-horizontal" action="updateclient.php?id=<?php echo $id?>" method="post">
  78.           <div class="control-group <?php echo !empty($firstnameError)?'error':'';?>">
  79.             <label class="control-label">First name</label>
  80.             <div class="controls">
  81.                 <input name="firstname" type="text"  placeholder="First name" value="<?php echo !empty($firstname)?$firstname:'';?>">
  82.                 <?php if (!empty($firstnameError)): ?>
  83.                     <span class="help-inline"><?php echo $firstnameError;?></span>
  84.                 <?php endif; ?>
  85.             </div>
  86.           </div>
  87.           <div class="control-group <?php echo !empty($lastnameError)?'error':'';?>">
  88.             <label class="control-label">Last name</label>
  89.             <div class="controls">
  90.                 <input name="lastname" type="text" placeholder="Last name" value="<?php echo !empty($lastname)?$lastname:'';?>">
  91.                 <?php if (!empty($lastnameError)): ?>
  92.                     <span class="help-inline"><?php echo $lastnameError;?></span>
  93.                 <?php endif;?>
  94.             </div>
  95.           </div>
  96.  
  97.               <div class="form-actions">
  98.                   <button type="submit" class="btn btn-success">Update</button>
  99.                   <a class="btn" href="client.php">Back</a>
  100.                 </div>
  101.             </form>
  102.         </div>
  103.  
  104.     </div> <!-- /container -->
  105.   </body>
  106. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement