简体   繁体   中英

search using a keyword and display all names that have that keyword in a table php

    <HTML>


<?php

include 'dbconfig.php';
$hotelname=$_GET['Hotel'];
$con = mysqli_connect($dbhostname, $dbusername, $dbpassword, $dbname) 
    or die("<br>Cannot connect to database!\n");

$query = "SELECT * FROM Hotel WHERE hotelname LIKE  '%".$hotelname."%'";
$result = mysqli_query($con, $query);

if($result) {
    echo "Brodley, Matt";
    echo "<b> </b>";
    echo "You are searching keywords: xxxx";
    echo "<TABLE BORDER=1>";
    echo "<tr><TD>hotelname<TD>city\n";
    while($row = mysqli_fetch_array($result)){
        $hotelname= $row['hotelname'];
        $city= $row['city'];
        echo "<tr><TD>$hotelname<TD>$city\n";
        }
    echo "</TABLE>";
}else echo "no hotel found for search keyword xxxx";

mysqli_free_result($result);
mysqli_close($con);

?>



</HTML>

So once I enter into the searched_word textbox on the previous page it should display a list of hotelnames that match the searched_word. Say when I search Omni it takes the searched_word but just displays

no hotel found for search keyword Omni

In my table that its database their is a hotelname that matches the searched_word

you may try replacing

if($result == searched_word){

by

if($result){ // if(mysqli_num_rows($result) > 0)

You can try the below code. Here I have added the statement to print the mysql query error and also removed "street" column in sub query.

<HTML>
        <?php
       include 'dbconfig.php';
       $con = mysqli_connect($dbhostname, $dbusername, $dbpassword, $dbname) 
        or die("<br>Cannot connect to database!\n");

        $query = "SELECT hotelname,city FROM Hotel WHERE city IN ( SELECT city FROM Branch WHERE hotelname LIKE '$_GET[searched_word]')";
        $result = mysqli_query($con, $query);

        if($result){
            echo "Brodley, Matt";
            echo "You are searching keywords: $_GET[searched_word]";
            echo "<TABLE BORDER=1>";
            echo "<tr><TD>hotelname<TD>city\n";
            while($row = mysqli_fetch_array($result)){
                $hotelname= $row['hotelname'];
                $city= $row['city'];
                echo "<tr><TD>$hotelname<TD>$city\n";
                }
            echo "</TABLE>";
        }
        else {
            echo "no hotel found for search keyword $_GET[searched_word]";
            printf("Errormessage: %s\n", mysqli_error($con));
        }

        mysqli_free_result($result);
        mysqli_close($con);

        ?>

        </HTML>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM