Squito

Untitled

Jul 4th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <title>Search Box</title>
  4.     <link rel="stylesheet" href="style.css" type="text/css">
  5. </head>
  6. <body>
  7.     <form action="" method="POST" id="searchForm">
  8.         <input type="text" name="q" id="searchBar" placeholder="Search..." maxlength="25">
  9.         <input type="submit" id="searchBtn" value="Go">
  10.     </form>
  11.     <?php
  12.         if(isset($_POST['q']))
  13.         {
  14.             if(trim($_POST['q'])!='')
  15.             {
  16.                 $con = mysqli_connect('localhost','root','','search');
  17.                 $prep=mysqli_stmt_init($con);
  18.                 mysqli_stmt_prepare($prep, "SELECT id, title, description FROM products WHERE title LIKE ? OR description LIKE ?");
  19.                 $q='%'.$_POST['q'].'%';
  20.                 mysqli_stmt_bind_param($prep, "ss", $q, $q);
  21.                 mysqli_stmt_execute($prep);
  22.                 mysqli_stmt_store_result($prep);
  23.                 $num_rows = mysqli_stmt_num_rows($prep);
  24.                 ?>
  25.                 <p><strong><?php echo $num_rows; ?></strong> results for '<?php echo htmlspecialchars($_POST['q']); ?>'</p>
  26.                 <?php
  27.                 if($num_rows!=0)
  28.                 {
  29.                     mysqli_stmt_bind_result($prep, $id, $title, $desc);
  30.                     while(mysqli_stmt_fetch($prep))
  31.                     {
  32.                         echo '<h3><a href="termek.php?id='.$id.'">'.$title.'</a><h3><p>'.nl2br($desc).'</p><br>';
  33.                     }  
  34.                 }
  35.                 mysqli_stmt_close($prep);
  36.             }
  37.         }
  38.     ?>
  39. </body>
  40. </html>
Add Comment
Please, Sign In to add comment