0

Doing a simple PHP/SQL search bar on my database and the results aren't appearing. The search bar appears, and whatever i type isn't appearing in the URL. Code is below. I'm connecting to a database through a different file.

Index.php

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <center>    		
        <form action="search.php" method="post">
		<input type="text" name="search" autocomplete="off">
        	<input type="submit" value="search">
        </form>
    </center>
</body>
</html>

search.php

<?php
	$search = $_GET['search'];
	require 'constants.php';
?>

<?php

$query = "SELECT Name, Zip, Address, Type FROM parks WHERE Zip = '%{$search}%'";

$result = mysqli_query($db_connection,$query);

while ($row = mysqli_fetch_array($result))
{
    // loop through output one row at a time
    $name = 		$row["Name"];
    $zip = 		$row["Zip"];
    $address = 		$row["Address";
    $type = 	$row["Type"];

    echo $name . $zip . $address . $type;
}

?>

1 Answer 1

7

First off, you explicitly set the method type as POST:

<form action="search.php" method="post">

then, you're trying to get values of:

<input type="text" name="search" autocomplete="off">

Thru $search = $_GET['search'];. Use $_POST['search'];

Second, this doesn't make sense

WHERE Zip = '%{$search}%'";

If you want to search with a wildcard, better use LIKE clause.

And why not use prepared statements:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

if(isset($_POST['search'])) {
    require 'constants.php';
    $search = '%' . $_POST['search'] . '%';
    $query = "SELECT Name, Zip, Address, Type FROM parks WHERE Zip LIKE ?";

    $select = $db_connection->prepare($query);
    $select->bind_param('s', $search);
    $select->execute();
    $select->store_result();

    if($select->num_rows > 0) {
        $select->bind_result($name, $zip, $address, $type);
        while($select->fetch()) {
            // loop through output one row at a time

            echo $name . $zip . $address . $type . '<br/>';
        }
    }

}
?>

Another way of fetching:

if(isset($_POST['search'])) {
    require 'constants.php';
    $search = '%' . $_POST['search'] . '%';
    $query = "SELECT Name, Zip, Address, Type FROM parks WHERE Zip LIKE ?";

    $select = $db_connection->prepare($query);
    $select->bind_param('s', $search);
    $select->execute();
    $results = $select->get_result();

    if($select->num_rows > 0) {
        while($row = mysqli_fetch_assoc($results)) {
            // loop through output one row at a time
            $name = $row["Name"];
            $zip = $row["Zip"];
            $address = $row["Address"];
            $type = $row["Type"];

            echo $name . $zip . $address . $type . '<br/>';
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

the "%search%" think was because i was trying to ass the search field from the index page through to the search page and also query based on what was typed. Still having trouble with this. a few things have been fixed, but I'm still getting nothing appearing.
@JoshuaMobley make sure your error reporting is turned on, i made some revisions, take a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.