搜索框没有结果,PHP SQL

时间:2013-12-24 13:59:10

标签: php mysql sql search if-statement

我正在尝试使用if语句来使搜索框正常工作,但由于某些原因,根本没有数据出现: 你能帮我纠正一下if语句,以使其正常工作。

<?php
if (isset($_POST['submit'])) {


//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);

   echo '<html><head><title>View Records</title><link rel="stylesheet" href="assets/layouts/reset.css" type="text/css" /><link rel="stylesheet" href="assets/layouts/portal/style.css" type="text/css" /><link rel="stylesheet" href="http://portal.gtlaw.co.uk/assets/layouts/portal/icons.css?r=IanKnowles-RULZ" type="text/css" /><link rel="stylesheet" href="http://portal.gtlaw.co.uk/assets/javascript/jquery/themes/base/jquery.ui.all.css?MH=1"><link rel="stylesheet" href="http://portal.gtlaw.co.uk/assets/javascript/jquery/themes/plugins/ui.dropdownchecklist.standalone.css"><script src="http://portal.gtlaw.co.uk/assets/javascript/jquery/jquery-1.7.1.js?MH=1"></script><script src="http://portal.gtlaw.co.uk/assets/javascript/jquery/ui/jquery.ui.core.js?MH=1"></script><script src="http://portal.gtlaw.co.uk/assets/javascript/jquery/ui/jquery.ui.widget.js?MH=1"></script><script src="http://portal.gtlaw.co.uk/assets/javascript/jquery/ui/jquery.ui.datepicker.js?MH=1"></script><script src="http://portal.gtlaw.co.uk/assets/javascript/jquery/ui/i18n/jquery.ui.datepicker-en-GB.js?MH=1"></script><script src="http://portal.gtlaw.co.uk/assets/javascript/jquery/ui/jquery.ui.dropdownchecklist.js"></script><script src="http://portal.gtlaw.co.uk/assets/javascript/site/site.js"></script>';

//check whether the name parsed is empty


//database connection info
$host = "localhost"; //server
$db = "hidden"; //database name
$user = "hidden"; //dabases user name
$pwd = "hidden"; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM usertracker WHERE firstname LIKE '%$searchTerm%' OR lastname LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
    $output = "";
    while($row = mysqli_fetch_array($results))
    {

                $output .= '<tr class="heads">';
                $output .= '<td>'  . $row['id'] . '</td>';
                $output .= "<td class=\"click\"><a href=\"viewtrackcase.php?id="  . $row['id'] . "\"><img src=\"assets/images/edit.png\" /> "   . $row['firstname'] .  " "  . $row['lastname'] .  "</a></td>";

$output .= '</tr>';

    }
$output .= '</table>';
    echo $output;
}

elseif($searchTerm == "")
{
    echo "Enter name you are searching for.";
    exit();
}
else
{


        // connect to the database
        include('connect-db.php');

        // number of results to show per page
        $per_page = 20;

        // figure out the total pages in the database
        $result = mysql_query("SELECT * FROM usertracker WHERE agent = '{$_SESSION['user']['username']}'   ORDER BY `submitdate` ASC");
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        // display pagination



        // display data in table

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // echo out the contents of each row into a table
               echo '<tr class="heads">';
                echo '<td>'  . mysql_result($result, $i, 'id') . '</td>';
                echo "<td class=\"click\"><a href=\"viewtrackcase.php?id="  . mysql_result($result, $i, 'id') . "\"><img src=\"assets/images/edit.png\" /> "  . mysql_result($result, $i, 'firstname') . " "  . mysql_result($result, $i, 'lastname') . "</a></td>";
                echo "<td>"  . mysql_result($result, $i, 'solicitor') . "</td>";
                echo "<td>"  . mysql_result($result, $i, 'status') . "</td>";
                                echo "<td>"  . mysql_result($result, $i, 'agent') . "</td>";
                                                echo "<td>"  . mysql_result($result, $i, 'submitdate') . "</td>";
                echo "</tr>"; 
        }
        // close table>


        // pagination
                        echo "<p><b>View Page: </b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='viewtracker.php?page=$i'>$i</a> ";
        }

        echo "&nbsp;&nbsp; | &nbsp;&nbsp; Total records: $total_results";
        echo "</p>";
}
}
?>

感谢任何形式的帮助。

0 个答案:

没有答案