Reputation: 17
I want the webpage to show the results once the search button is clicked. Currently, the whole table is displayed while the page is loading. If I change the value of the variable $sql from “Select * FROM users” then the search query doesn’t work anymore
I’m using XAMPP with webpage being a php file and the database is Mariadb
+--------+-------------+---------------+--------------------------+--------+
| ID | firstname | lastname | address | count |
| | | | | |
+--------------------------------------------------------------------------+
| 1 | john | doe |james street, idaho, usa | 2 |
| | | | | |
+--------------------------------------------------------------------------+
| 2 | cindy | smith |rollingwood av,lyn, canada| 1 |
| | | | | |
+--------------------------------------------------------------------------+
| 3 | rita | chatsworth |arajo ct, alameda, cali | 0 |
| | | | | |
+--------------------------------------------------------------------------+
| 4 | randy | plies |smith spring, lima, (peru)| 1 |
| | | | | |
+--------------------------------------------------------------------------+
| 5 | Matt | gwalio |park lane, (atlanta), usa | 2 |
| | | | | |
+--------------------------------------------------------------------------+
The webpage with search bar.
<?php
ini_set('memory_limit', '1042M');
$localhost = "localhost";
$username = "root";
$password = "";
$dbname = "samueldb";
$con = new mysqli($localhost, $username, $password, $dbname);
if( $con->connect_error){
die('Error: ' . $con->connect_error);
}
$sql = "SELECT * FROM users";
if( isset($_GET['btn']) ){
$name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
$sql = "SELECT * FROM 'users' WHERE 'firstname' ='$name'";
}
$result = $con->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Find my search results</title>
<link href="css/templatemo-style.css" rel="stylesheet" />
<link href="css/font-awesome.min.css" rel="stylesheet" />
<link href="css/font-awesome.css" rel="stylesheet" />
<link href="css/bootstrap.css" rel="stylesheet" />
<link href="css/search-style.css" rel="stylesheet" />
</head>
<body>
<h2>List of students</h2>
<div class="search">
<input type="text" class="searchTerm" placeholder="What are you looking for?">
<button type="search" class="searchButton">
<i class="fa fa-search"></i>
</button>
</div>
</div>
<table class="table table-striped table-dark">
<tr>
<th>ID</th>
<th>First_Name</th>
<th>Address</th>
<th>Count</th>
</tr>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['count']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
Upvotes: 0
Views: 176
Reputation: 5288
There are some issues going on to your code.
form
. You forgot to wrap your inputs inside a form
tag. You should also add a name="search"
on your input, and type="search"
on button
doesn't make sense. Change your form to<form>
<input type="search" name="search" class="searchTerm" placeholder="What are you looking for?">
<button type="submit" name="btn" class="searchButton">
<i class="fa fa-search"></i>
</button>
</form>
name
attributes should match on your $_GET[name]
.
$sql = "SELECT * FROM users WHERE firstname = '$name'";
Upvotes: 1