Advertisement
Guest User

skrrrr

a guest
Mar 22nd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. <?php
  2.         $servername = "localhost";
  3.         $username = "root";
  4.         $password = "";
  5.         $dbname = "firsttest";
  6.        
  7.        
  8.         if(isset($_POST['Searchh'])) {
  9.            
  10.             $searchword = $_POST["Searchh"];
  11.             // Create connection
  12.         $conn = new mysqli($servername, $username, $password, $dbname);
  13.         // Check connection
  14.         if ($conn->connect_error) {
  15.             die("Connection failed: " . $conn->connect_error);
  16.         }
  17.  
  18.         $sql = "SELECT ISBN, Title, Author FROM books where Author = '$searchword'";
  19.         $result = $conn->query($sql);
  20.         } else {
  21.  
  22.         // Create connection
  23.         $conn = new mysqli($servername, $username, $password, $dbname);
  24.         // Check connection
  25.         if ($conn->connect_error) {
  26.             die("Connection failed: " . $conn->connect_error);
  27.         }
  28.  
  29.         $sql = "SELECT ISBN, Title, Author FROM books";
  30.         $result = $conn->query($sql);
  31.         }
  32.         ?>
  33.        
  34. <!DOCTYPE HTML>
  35. <HTML>
  36.    <HEAD>
  37.    <style>
  38.     table, th, td {
  39.     border: 1px solid black;
  40.     }
  41. </style>
  42.       <TITLE>My first HTML document</TITLE>
  43.    </HEAD>
  44.    <BODY>
  45.    </table>
  46.             <form action="test.php" method = "POST">
  47.                 Search by Author: <input type="text" name="Searchh"><br>
  48.             <input type="submit" value="Submit">
  49.             </form>
  50.        
  51.        
  52.             <table style="width:100%">
  53.             <tr>
  54.                 <td>ISBN</td>
  55.                 <td>Title</td>
  56.                 <td>Author</td>
  57.             </tr>
  58.            
  59.         <?php
  60.         if ($result->num_rows > 0) {
  61.             // output data of each row
  62.             while($row = $result->fetch_assoc()) {
  63.                 //echo "ISBN: " . $row["ISBN"]. " - Title: " . $row["Title"]. " - Author: " . $row["Author"]. "<br>";
  64.                 echo "<tr>";
  65.                 echo "<td>".$row['ISBN']."</td>";
  66.                 echo "<td>".$row['Title']."</td>";
  67.                 echo "<td>".$row['Author']."</td>";
  68.                 echo "</tr>";
  69.             }
  70.         } else {
  71.             echo "0 results";
  72.         }
  73.         $conn->close();
  74.         ?>
  75.        
  76.    </BODY>
  77. </HTML>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement