Error message if no search record found

ساخت وبلاگ
Hi, I need some advice. I have a search form and it is able to do a search. But when there is no serach record found, it will show this message "Notice: Undefined variable: returnArray in C:xampphtdocs....". I need advice on how to prevent from showing the error messgae. Thanks

PS: I only input the part of the code that I having issue and not the full code.

Below is my code for search page
Code:

<fieldset>
    <form name="search" method="post" action="doSearch.php">
        <font face="Arial" size="normal">
        <table style="width: 50%;">
            <tr>
                <td colspan="2" align="center"><h3>Search Car</h3></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Brand</td>
                <td><input type="text" name="brand" size="20" style="width: 270px" placeholder="Brand"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>

            <tr>
                <td>Model</td>
                <td><input type="text" name="model" size="20" style="width: 270px" placeholder="Model"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>


            <tr align="center">
                <td><input type='submit' value='Search'></td>
                <td><input type='reset' value='Reset'></td>
            </tr>

        </table>
        </font>
    </form>
</fieldset>

This is my doSearch page
Code:

<?php
include 'dbConfig.php';

$brand = $_POST['brand'];
$model = $_POST['model'];

// if all fields are empty
if ($brand == NULL && $model == NULL) {
    echo '<script type="text/javascript">alert("No Input"); window.location="search.php";</script>';

    // Search by brand
} else if ($brand != NULL && $model == NULL) {
    $query1 = executeSelectQuery("SELECT * FROM cardetails cd, brand b, type t, model m where cd.brandName = b.brandID and cd.type = t.typeID and cd.model = m.modelID and b.brandName LIKE '%$brand%'");

    // Search by model
} else if ($brand == NULL && $model != NULL) {
    $query1 = executeSelectQuery("SELECT * FROM cardetails cd, brand b, type t, model m where cd.brandName = b.brandID and cd.type = t.typeID and cd.model = m.modelID and m.model LIKE '%$model%'");

    // Search all fields
} else if ($brand != NULL && $model != NULL) {
    $query1 = executeSelectQuery("SELECT * FROM cardetails cd, brand b, type t, model m where cd.brandName = b.brandID and cd.type = t.typeID and cd.model = m.modelID and m.model LIKE '%$model%' and b.brandName LIKE '%$brand%'");
}
// if no result is found
if (count($query1) == NULL) {
    echo '<script type="text/javascript">alert("No Record Found"); window.location="search.php";</script>';
} else {
    ?>

    <fieldset >
        <legend><b>Search Results</b></legend>
        <table border="1" width="100%">

            <tr>
                <th>Brand</th>
                <th>Type</th>
                <th>Model</th>
                <th>Price</th>
                <th>Descriptions</th>
            </tr>

            <?php
            for ($countUsers = 0; $countUsers < count($query1); $countUsers++) {
                $brand = $query1[$countUsers]['brandName'];
                $model = $query1[$countUsers]['model'];
                $type = $query1[$countUsers]['type'];
                $price = $query1[$countUsers]['price'];
                $details = $query1[$countUsers]['details'];
                ?>

                <tr>
                    <td align="center"><?php echo $brand; ?></td>
                    <td align="center"><?php echo $type; ?></td>
                    <td align="center"><?php echo $model; ?></td>
                    <td align="center"><?php echo $price; ?></td>                                     
                    <td align="center"><?php echo $details; ?></td>
                    <?php
                }
                ?>
            </tr>

        </table>
    </fieldset>

    <?php
}
?>

<br/>
<form name="form3" Action="search.php" Method="post">
    <input name="submit" value="Back to Search" type="submit">
</form>

Below is my dbConfig.php page
Code:

<?php
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'cars';

$link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB) or die(mysqli_connect_error());

function executeSelectQuery($query) {
    $result = mysqli_query($GLOBALS['link'], $query) or die(mysqli_error($GLOBALS['link']));
    while ($row = mysqli_fetch_array($result)) {
        $returnArray[] = $row;
    }
    return $returnArray;
}

function executeDeleteQuery($query) {
    return mysqli_query($GLOBALS['link'], $query) or die(mysqli_error($GLOBALS['link']));
}

function executeInsertQuery($query) {
    return mysqli_query($GLOBALS['link'], $query) or die(mysqli_error($GLOBALS['link']));
}

function executeUpdateQuery($query) {
    return mysqli_query($GLOBALS['link'], $query) or die(mysqli_error($GLOBALS['link']));
}

?>

CodingForums...
ما را در سایت CodingForums دنبال می کنید

برچسب : نویسنده : codingforums بازدید : 186 تاريخ : شنبه 21 بهمن 1396 ساعت: 5:39