Advertisement
Guest User

PHP Project 1

a guest
Apr 20th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1.  
  2. <!--PHP BLOCK-->
  3. <?php
  4.  
  5. // If we are searching the database
  6. if(isset($_POST['search']))
  7. {
  8.  
  9.     // Get the value from the search form
  10.     $valueToSearch = $_POST['valueToSearch'];
  11.  
  12.     // search in all table columns using concat mysql function
  13.     $query = "SELECT * FROM `VAR_table_name` WHERE CONCAT(`VAR_id`, `VAR_column`, `VAR_column`, `VAR_column`) LIKE '%".$valueToSearch."%'";
  14.     $search_result = filterTable($query);
  15.  
  16. }
  17.  
  18. // we are not searching, get everything from the database
  19. else {
  20.     $query = "SELECT * FROM `VAR_table_name`";
  21.     $search_result = filterTable($query);
  22. }
  23.  
  24. // function to connect and execute the query
  25. function filterTable($query)
  26. {
  27.     // 1. Create a database connection
  28.     $dbhost = "ecsmysql";
  29.     $dbuser = "cs332u14";  // where ?? is your id
  30.     $dbpass = "oodokahc"; // replace with your password
  31.     $dbname = "cs332u14";
  32.     $dbconnection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  33.  
  34.     // 2. Check if the connection is ok
  35.     if (mysqli_connect_errno()) {
  36.         die("Database connection failed: " .
  37.             mysqli_connect_error() . " (" > mysqli_connect_errno() . ")" );
  38.     }
  39.  
  40.     // 3. Execute the query
  41.     $filter_Result = mysqli_query($dbconnection, $query);
  42.  
  43.     // Check if there is a query error
  44.     if (!$filter_Result) {
  45.         die("Database query failed.");
  46.     }
  47.  
  48.     // Return the query result
  49.     return $filter_Result;
  50. }
  51. ?>
  52. <!--END OF PHP BLOCK-->
  53.  
  54.  
  55. <!--HTML BLOCK-->
  56. <!DOCTYPE html>
  57. <html>
  58. <head>
  59.     <title>TABLE DATA SEARCH</title>
  60.     <style>
  61.         table,tr,th,td
  62.         {
  63.             border: 1px solid black;
  64.         }
  65.     </style>
  66. </head>
  67. <body>
  68.  
  69. <!--Search Form-->
  70. <form action="table_data_filter.php" method="post">
  71.  
  72.     <!--Edit text box-->
  73.     <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
  74.  
  75.     <!--Sumbit Button-->
  76.     <input type="submit" name="search" value="Filter"><br><br>
  77.  
  78.     <!--Data Table-->
  79.     <table>
  80.         <tr>
  81.             <th>Id</th>
  82.             <th>VAR_column</th>
  83.             <th>VAR_column</th>
  84.             <th>VAR_column</th>
  85.         </tr>
  86.  
  87.         <!-- Populate table from mysql database -->
  88.         <?php while($row = mysqli_fetch_array($search_result)):?>
  89.             <tr>
  90.                 <td><?php echo $row['id'];?></td>
  91.                 <td><?php echo $row['VAR_column'];?></td>
  92.                 <td><?php echo $row['VAR_column'];?></td>
  93.                 <td><?php echo $row['VAR_column'];?></td>
  94.             </tr>
  95.         <?php endwhile;?>
  96.     </table>
  97. </form>
  98.  
  99. </body>
  100.  
  101.  
  102. <!--Cleanup-->
  103. <?php
  104. // 4. Release returned result
  105. mysqli_free_result($search_result);
  106. ?>
  107.  
  108. <?php
  109. // 5. Close the database connection
  110. mysqli_close($dbconnection);
  111. ?>
  112.  
  113. </html>
  114. <!--END OF HTML BLOCK-->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement