Sorry if the answer for my problem exists in another location I had a search and was unable to find it.
I have recently been learning how to design webpages and decided that I wanted to put a search bar on a website and thus followed a tutorial on youtube. I followed it exactly as the producers produced the video and with that everything worked. However, as I will need to be able to put a search function into other websites I also created another database with another table using phpmadmin through the use of WAMP.
Database is named ‘app’ and the table is named ‘people’
The search function is very simple and consists of two php files which are named index-search.php and search.php
Copies of the file contents are - * note I took the second > from tags as it did not show in preview so you can see the code. They do exist in my php script.
index-search.php
html
<head
<title Search-Home </title
</head
<body
<form action="./search.php" method="get"
<input type="text" name="q" dir="ltr"
<input type="submit" value="go"
</form
</html
search.php
<?php
$conn = mysqli_connect("localhost", "root", "", "app");
if(mysqli_connect_errno())
{
echo "Failed to connect: " . mysqli_connect_error();
}
?>
<?php
//error_reporting(0);
$output = ' ';
if(isset($_GET ['q']) && $_GET['q'] !== ' ')
{
$searchq = $_GET['q'];
$q = mysqli_query($conn, "SELECT * FROM search WHERE keywords LIKE '%$searchq%' OR first_name LIKE '%$searchq% '") or die (mysqli_error($conn));
$c = mysqli_num_rows($q);
if($c == 0)
{
$output = 'No search results for <b>"' . $searchq . '"</b>';
echo 'NO data found';
}
else
{
while($row = mysqli_fetch_array($q))
{
$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$bio = $row['bio'];
$created = $row['created'];
$output .= '<a href=" ' . $id . '">
<h3> ' . $first_name . '</h3>
<p> ' . $last_name . '</p>
<p> ' . $bio . '</p>
<p> ' . $created . '</p>
</a>';
}
}
}
else
{
header("location: ./");
}
print("$output");
mysqli_close($conn);
?>
I believe the problem is in relation to the search.php file this is because as noted above when I directly followed the tutorial including the databases shown named ‘tutorial’ with a table named ‘search’ it works. With this one I renamed the search.php to searchs.php and when I change the line in the 'index-search.php to the search query works but it connects to the tutorial database.
Not sure what I am doing wrong.
- The database and table exit.
- The file search.php exists.
If anyone can give me any ideas it would be really helpful.