Search Contacts Table Demo

Search Field:
Search Text:
 

CODE FOLLOWS

<?php
    error_reporting
(E_ALL);

    
$self basename($_SERVER['SCRIPT_NAME']);
    
$step = isset($_POST['step']) ? $_POST['step'] : 0;
    
$search_field = isset($_POST['search_field']) ? $_POST['search_field'] : null;
    
$search_text = isset($_POST['search_text']) ? $_POST['search_text'] : null;

    include(
'includes/open_db.php'); // Database connection

    // Check database connection
    
if (!$con)
    {
        die(
"Error: Unable to connect to the database.");
    }

    
// Fetch field names
    
$field = [];
    
$field_text = [];
    
$query "SHOW COLUMNS FROM contacts FROM $db_name";
    
$result mysqli_query($con$query) or die("Error fetching columns: " mysqli_error($con));

    while (
$row mysqli_fetch_array($result))
    {
        
$field[] = $row[0];
        
$field_text[] = ucwords(str_replace('_'' '$row[0]));
    }
    include(
'includes/close_db.php');
    include(
'includes/header.php');
    
?>

    <h2>Search Contacts Table Demo</h2>

    <form action="<?php echo $self?>" method="post">
        <table class="full">
            <tr>
                <td class="right leftside">Search Field:</td>
                <td class="left rightside">
                    <select name="search_field">
                        <?php foreach ($field_text as $key => $value): ?>
                            <option value="<?= htmlspecialchars($field[$key]); ?><?= $field[$key] == $search_field 'selected' ''?>>
                                <?= htmlspecialchars($value); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="right leftside">Search Text:</td>
                <td class="left rightside">
                    <input type="text" size="43" id="search_text" name="search_text" value="<?= htmlspecialchars($search_text); ?>">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="hidden" name="step" value="1">
                    <input type="submit" class="button" name="submit" value="Search">
                </td>
            </tr>
        </table>
    </form>

    <?php if ($step == 1): ?>

        <?php include('includes/open_db.php'); ?>

        <?php

        $search_field 
mysqli_real_escape_string($con$search_field);
        
$search_text "%" mysqli_real_escape_string($con$search_text) . "%";
        
$query "SELECT * FROM contacts WHERE `$search_field` LIKE ? ORDER BY `$search_field`";
        
$stmt $con->prepare($query);
        
$stmt->bind_param("s"$search_text);
        
$stmt->execute();
        
$result $stmt->get_result();
        
$total $result->num_rows;

        
?>

        <h2>Results of Search: <?= $total?> Records</h2>

        <?php if ($total 0): ?>

            <table class="full">

                <tr class="header2">
                    <?php foreach ($field_text as $header): ?>
                        <th><?= htmlspecialchars($header); ?></th>
                    <?php endforeach; ?>
                </tr>

                <?php while ($row $result->fetch_assoc()): ?>

                    <tr>
                        <?php foreach ($field as $col): ?>
                            <td><?= htmlspecialchars($row[$col]); ?></td>
                        <?php endforeach; ?>
                    </tr>

                <?php endwhile; ?>

            </table>

        <?php else: ?>
            <p class="red center">NO RECORDS FOUND!</p>
        <?php endif; ?>

        <?php include('includes/close_db.php'); ?>
<?php 
endif; ?>

<?php include('includes/footer.php'); ?>