1

I am attempting to make a search bar on my website that will query mysql database and return the results in a fashionable way. The search will be used to look for names. For instance, the user can type in "John Doe" and any information on John Doe will be returned. Unfortunately, whenever I click the submit button and my site transitions to 'search.php' the page is completely blank. I have tested my connection to the database and know that it is working. Is there something wrong with my code such that absolutely nothing shows up? It does not even revert the user back to the index.html.

I have a search bar on my index.html as follows:

       <form id="navsearch" class="form-group" role="search" action="search.php" method="post">
            <div class="input-group" display="inline">
                <input name="search" type="text" value="Search" class="form-control" placeholder="Search">
                <span class="input-group-btn">
                    <button class="btn btn-default btn" name="submit" class="btn btn-lg" type="submit">
                        <span class="glyphicon glyphicon-search" name="submit"></span>
                    </button>    
            </div>  
        </form>

Here is the php on 'search.php':

 <?php

 //make connection to database

  $connection = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306);

 $search_word = '';
 if (isset($_POST['submit'])) {
   if (!empty($_POST['search'])) {
    $search_word = $_POST['search'];
    $search = mysqli_real_escape_string($connection,      $_POST['search']);

    $query = "SELECT * FROM data WHERE name LIKE '%$search%' ";
    $search_query = mysqli_query($connection, $query);

    // START RESULTS

    if ($result = mysqli_query($connection, $query)) {
        $to_display = "<div>";


        while ($row = mysqli_fetch_array($result)) {
            if ((substr($row['file'], -3) == 'jpg') || (substr($row['file'], -3) == 'JPG') || (substr($row['file'], -3) == 'PNG') || (substr($row['file'], -3) == 'png') || (substr($row['file'], -3) == 'GIF') || (substr($row['file'], -3) == 'gif'))
                $img_url = "uploads/" . $row['file'];
            else {
                if ((strtolower(substr($row['file'], -3) == 'png')) || (strtolower(substr($row['file'], -3) == 'doc')) || (strtolower(substr($row['file'], -3) == 'docx') ))
                    $img_url = "uploads/pdf_logo.png";
                else
                    $img_url = "uploads/no_doc.png";
            }
            $to_display .= "<a href='story.php?id=".$row['id']."'  class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'><img alt='' class='img-responsive' src='" . $img_url . "'><div class='portfolio-box-caption'>
                            <div class='portfolio-box-caption-content'>
                                <div class='project-category text-faded'>
                                    Read
                                </div>
                                <div class='project-name'>"
                        . $row['name'] . "'s Story
                                </div>
                            </div>
                        </div>" . $row['name'] . " - " . $row['email'] . "</a>";

        }
    }

    $to_display .= "</div><div style='clear:both;'></div>";
} else {
    header("Location: index.html");
   }
 }

?>
Lauren
  • 87
  • 1
  • 2
  • 11
  • 1
    Try [enabling error reporting](http://stackoverflow.com/a/21429652/747654) in your PHP installation and run the script again – William Nov 06 '15 at 22:30
  • 2
    You are using a button, of type `submit` which will by it's very nature submit the form - which is set as`search.php` - what did you expect to happen? If you want an asynchronous, single page, no reload experience for your users you will need to use ajax to run the query without reloading the page – Professor Abronsius Nov 06 '15 at 22:32
  • Remove `if (isset($_POST['submit'])) { ... }` – c4pricorn Nov 06 '15 at 22:33
  • 1
    You assign `$search_query` to the `mysqli_query` yet you use `$results` in your `while` loop - they should be the same. – Professor Abronsius Nov 06 '15 at 22:47

1 Answers1

0

I'm far from sure what is meant by a fashionable way when it comes to submitting a form - the best I can think is that it refers to an ajax request that submits the search without reloading the page and the results are then somehow shown to the user on the same page? As there is no evidence of ajax it is hard to tell.

As I mentioned in a comment, your query refers to $search_query but the iteration through the recordset refers to $result - these need to be the same variable so that is probably why you were facing a blank screen - that and the fact the variable $to_display was never echoed out to the browser.

When in doubt use error_reporting to highlight where things might be going wrong.

<?php
    /* Sessions? */
    session_start();

    error_reporting( E_ALL ); /* Change this for live site! */

    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        $conn = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306);
        $exts_img=array('jpg','png','gif','jpeg');
        $exts_doc=array('pdf','doc','docx');
        $html=array();  /* use this to construct output. More efficient that string concatenation. */

        if ( isset( $_POST['submit'] , $_POST['search'] ) && !empty( $_POST['submit'] ) && !empty( $_POST['search'] ) ) {

            /* It is better to use prepared statements or pdo */
            $search = mysqli_real_escape_string( $conn,$_POST['search'] );  
            $sql = "SELECT * FROM `data` WHERE `name` LIKE '%".$search."%';";
            $results = mysqli_query( $conn, $sql );


            if( $results && mysqli_num_rows( $results ) > 0 ){
                $html[]="<div>";

                while( $row = mysqli_fetch_object( $results ) ) {

                    $id     =   $row->id;
                    $file   =   $row->file;
                    $email  =   $row->email;
                    $name   =   $row->name;
                    $ext    =   strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );



                    if( in_array( $ext, $exts_img ) ) {
                        $img_url = "uploads/" . $file;
                    } else {
                        if( in_array( $ext, $exts_doc ) ) $img_url='uploads/pdf_logo.png';
                        else $img_url='uploads/no_doc.png';
                    }


                    $html[]="
                        <a href='story.php?id=".$id."' class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'>
                            <img alt='' class='img-responsive' src='" . $img_url . "' />
                            <div class='portfolio-box-caption'>
                                <div class='portfolio-box-caption-content'>
                                    <div class='project-category text-faded'>Read</div>
                                    <div class='project-name'>{$name}'s story</div>
                                </div>
                            </div>
                            {$name} - {$email}
                        </a>";
                }
                $html[]="</div><div style='clear:both'></div>";
            }
            /* No results */
            $html[]='<h1>Sorry, no results...</h1>';

        } else {
            header("location: index.html");
        }

        /* free resources */
        mysqli_free_result( $results );
        mysqli_close( $conn );  

        /* output stuff to the browser */
        echo implode( PHP_EOL, $html );
    }
?>
Professor Abronsius
  • 26,348
  • 5
  • 26
  • 38