-3

I'm trying to create a search bar for my website on my local server, but when I submit the page generated is just blank. I've been following a couple of guides online but can't seem to figure out why it is not connecting to my MySQL db and getting the results. This is my first time attempting db and PHP so appreciate all advice.

<form action="./search.php" method="get">
        <input type="text" name="q">
        <input type="submit" value="Search">
</form>

Search.php

<?php

    $conn = mysqli_connect("localhost", "root", "root", "womendig_search");

    if(mysqli_connect_errno()){
        echo "Failed to connect: " . mysqli_connect_error(); 
    }

    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 title LIKE '%$searchq%'") or die(mysqli_error());
        $c = mysqli_num_rows($q);
        if($c == 0){
            $output = 'No search results for <b>"' . $searchq . '"</b>';
        } else {
            while($row = mysqli_fetch_array($q)){
                $id = $row['id'];
                $city = $row['city'];
                $country = $row['country'];
                $descriptions = $row['descriptions'];

                $output .= '<h3>' . $title . '</h3>
                                <p>' . $desc . '</p>
                            ';
            }
        }
    } else {
        header("location: ./");
    }
    print("$output");
    mysqli_close($conn);

?>
xtjnrchris
  • 45
  • 1
  • 8
  • Start by [turning on errors](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Machavity Jun 20 '16 at 16:39
  • A blank page is usually an error. Have you checked your error logs? You also are open to SQL injections, which might be causing your query to fail because it would be invalid.. – chris85 Jun 20 '16 at 16:41
  • So in your `$output`, you're appending `$title` and `$desc`. Where do those get set? – Chris Forrence Jun 20 '16 at 16:47

1 Answers1

0

The $title and $desc were not defined so you have empty <h3> and empty <p> tags.

Also i think it's better to make a few changes in your code.

Use !empty($_GET['q']) instead of $_GET['q'] !== ' ' and use extract($row); instead of

$id = $row['id'];
$city = $row['city'];
$country = $row['country'];
$descriptions = $row['descriptions'];