Implementation of search function on PHP bulletin board

Aug 24, 2020 PHP MySQL Beginner Beginner Engineer

Looking back on the bulletin board created in PHP, because I implemented the search function.

Click here for the referenced article. https://www.sejuku.net/blog/104455

The specific function is When you search for a name, the ID and content of the message associated with it are displayed. Shape.

Input form of search contents on top page → Create another file search function (search.php) I went there.

#Create search form I made it on the TOP page like this. The function actually works is search.php. Here, the input form receives characters, and the name “search_name” is received.

<h2>Search</h2>
<form action="search.php" method="post">
    <!-- Arbitrary <input> element = input field etc. -->
    <input type="text" name="search_name">
    <!-- Prepare submit button -->
    <input type="submit" name="submit" value="Search for name">
</form>

#Create search function The search function is divided into the first half of the DB connection and the second half that displays the retrieved characters.

The first half of searching arbitrary data from DB.

<?php
try{
    // connect to DB
    $dsn ='mysql:dbname=test_bbs2; host=localhost';
    $username='root';
    $password='root';
    $pdo = new PDO($dsn, $username, $password);

    //Execute the SQL statement and assign the result to $stmt.
    $stmt = $pdo->prepare(" SELECT * FROM contacts WHERE submit_name LIKE'%" .$_POST["search_name"] ."%' ");

    //Execute
    $stmt->execute();
    echo "OK";
    echo "<br>";
} catch(PDOException $e){
    echo "Failed:" .$e->getMessage() ."\n";
    exit();
}
?>

Omitted for DB connection. What matters is

    $stmt = $pdo->prepare(" SELECT * FROM contacts WHERE submit_name LIKE'%" .$_POST["search_name"] ."%' ");

Part of here.

"SELECT * FROM contacts WHERE submit_name LIKE'%" .$_POST["search_name"] ."%' "

In the SQL statement here ・From the contacts table (FROM contacts) ・Submit_name condition (WHERE submit_name) ・Get the value that includes includes $_POST[“search_name”]

If there is no% before or after $_POST[“search_name”] or LIKE, When there is a name Tanaka, you cannot hit Tanaka with the single character “Tana”. A situation occurs in which the search is not performed unless all “Tanaka” matches.

For more information on using %(wildcard) https://qiita.com/ika_katsuo/items/40e7784284344c7d8697 Refer to this article.

Search by LIKE is called fuzzy search.

The latter part that displays the data acquired from #DB on the screen. The data fetched from DB is described in HTML and PHP and displayed on the screen. The code looks like this.

<html>
    <body>
        <table>
            <tr><th>ID</th><th>Name</th><th>remark</th></tr>
            <!-- Now loop the result using PHP foreach -->
            <?php foreach ($stmt as $row): ?>
            <tr>
                <td><?php echo $row[0]?></td>
                <td><?php echo $row[1]?></td>
                <td><?php echo $row[2]?></td>
            </tr>

                <?php endforeach; ?>
        </table>
    </body>
</html>

Here, it is described in html, and the value is extracted from the stmt object by foreach.

・Use $row[0] for ID

・Name with $row[1],

・Use $row[2]

Display each.

After that, the table tag is used so that it will be in the form of a table.

that’s all.