Reputation: 1
I'm trying to get the php code to search the database and return all the matching "park_name"s but it says that the search variable is undefined and also only returns one park from the database. This is the code I have for the search:
<form method="post" action="Search_page.php" name="search" id="Search">
<label for="search">Search:</label><input type="text" name="Search" id="search" />
<input type="submit" name="submit" value="Search"/>
</form>
<?php
if(isset($_POST['search'])){
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search); }
$sql="SELECT Park_name, street FROM park_list WHERE park_name LIKE '%$search%'";
//query db
$result = $db->query($sql);
?>
</div>
<?php while ($row = $result->fetch_assoc()) { ?>
<div class="results">
<h2><?php echo $row['Park_name']?></h2> </br>
<p><?php echo $row['street']?></p>
</div>
<?php } ?>
Upvotes: 0
Views: 108
Reputation: 74217
Because, Search
!= search
.
Error reporting told you about it too.
!=
is the logical operator for "does not equal" ;-)Those are case-sensitive.
By the way; do yourself a favor and use a prepared statement if you want to save/keep your database.
and check for errors on the query, should it fail using mysqli_error($db)
.
You're also using a name attribute here in conjunction with the POST array of the same name:
<form method="post" action="Search_page.php" name="search" id="Search">
^^^^^^^^^^^^^
Remove that ^
and rename name="Search"
for the input to name="search"
.
where you thought would pan out, which it won't. Your search is relying on the input's name attribute (and the input itself). Forms generally do not use name attributes.
Side note: It's usually best to use a !empty()
< (not empty) for a user input, instead of isset()
. The latter is mostly used for radios/checkboxes/submit inputs.
Upvotes: 3
Reputation:
I don't have rep to comment yet, but Park_name should be lowercase. You have inconsistent case in the sql statement:
$sql="SELECT Park_name, street FROM park_list WHERE park_name LIKE '%$search%'";
Upvotes: 0
Reputation: 1703
Form field names are case sensitive.
Change your second line to
<label for="search">Search:</label><input type="text" name="search" id="search" />
Upvotes: 1