Advertisement
Guest User

TJ Le

a guest
Aug 16th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Search people</title>
  5.     </head>
  6.     <body>
  7.         <?php require 'db/connect.php'; ?>
  8.  
  9.         <h3>Search people</h3>
  10.  
  11.         <?php
  12.  
  13.             $records    = array();
  14.             $errors     = array(); 
  15.  
  16.             // If button Search is pressed
  17.             if (isset($_POST['search'])){
  18.  
  19.                 // Grab data from the field search_fn
  20.                 $search_fn = trim($_POST['search_fn']);
  21.  
  22.                 // If field search_fn is empty
  23.                 if (empty($search_fn)){
  24.  
  25.                     $errors[] = '<font color="red"><strong>Error:</strong> Field search is important</font><br /><br />';
  26.                 } else {
  27.  
  28.                     // Field search_fn should cointaint at least 3 chars
  29.                     if (strlen($search_fn) < 3){
  30.  
  31.                         $errors[] = '<font color="red"><strong>Error:</strong> Field search should cointaint at least 3 chars!</font><br /><br />';
  32.                     } else {
  33.  
  34.                         // Search database
  35.                         if ($results = $db->query("SELECT * FROM `people` WHERE `first_name` LIKE '%{$search_fn}%'")){
  36.  
  37.                             if ($results->num_rows){
  38.  
  39.                                 while ($row = $results->fetch_object()){
  40.  
  41.                                     $records[] = $row;
  42.                                 }
  43.  
  44.                                 $results->free();
  45.                             } else {
  46.  
  47.                                 $errors[] = '<font color="red"><strong>Error:</strong> User doesn\'t exist!</font><br /><br />';
  48.                             }
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.  
  54.         ?>
  55.  
  56.         <form method="post" action="">
  57.             Search by First name: <input type="text" name="search_fn">
  58.             <input type="submit" name="search" value="Search">
  59.         </form><!-- end form -->
  60.  
  61.         <?php
  62.             echo '<hr />';
  63.             foreach ($records as $r){
  64.  
  65.                 echo $r->first_name,' ',$r->last_name,' ',$r->created, '<br />';
  66.             }
  67.  
  68.             echo implode($errors);
  69.  
  70.         ?>
  71.     </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement