-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
37 lines (31 loc) · 839 Bytes
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
session_start();
// Database connection
$host = "localhost:3306";
$db = "shop_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$searchQuery = '';
$results = [];
if (isset($_GET['query'])) {
$searchQuery = $_GET['query'];
$stmt = $conn->prepare("SELECT * FROM products WHERE title LIKE ? OR description LIKE ?");
$likeQuery = '%' . $searchQuery . '%';
$stmt->bind_param('ss', $likeQuery, $likeQuery);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
$stmt->close();
}
$_SESSION['search_results'] = $results;
$_SESSION['search_query'] = $searchQuery;
$conn->close();
header('Location: index.php');
exit();
?>