简体   繁体   中英

If the user typed * as the keyword, display all the entries in the table

I'm new to PHP and not sure how to use preg_match(), can you help me to modify my code that when user enters * displays it will displays the entire table in alphabetical ascending order? I appreciate any help .

<html>
<head>
    <meta charset="utf-8">

</head>
<body id="main_body" onload="FP_preloadImgs(/*url*/'images/button16.jpg', /*url*/'images/button19.jpg')" >

<img id="top" src="images/top.png" alt="">
<div id="form_container">

    <h1><a href="">Lab</a></h1>

    <div align="right">
        <table border="0" cellpadding="0" cellspacing="0" width="25%">
            <tr>
                <td width="10" align="center"></td>

                <td width="10" align="center"><a href="search.php">
                        <img border="0" id="img1" src="images/button14.jpg" height="20" width="100" alt="Search" onmousedown="FP_swapImg(1,0,/*id*/'img1',/*url*/'images/button16.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img1',/*url*/'images/button14.jpg')" fp-style="fp-btn: Linked Row 8; fp-img-hover: 0" fp-title="Search"></a></td>
            </tr>
        </table>
        <div id="form_container" style="align-content: center ;height: min-height:50%px">


            <ul >

        <?php
                $ID = $_POST["ID"];
                $FIRST_NAME = $_POST["FIRST_NAME"];
                $LAST_NAME = $_POST["LAST_NAME"];
                $URL = $_POST["URL"];
                $DESCRIPTION = $_POST["DESCRIPTION"];
                $search = $_POST ['search'];



                // Connect to MySQL
            if(!($sqlCon= mysql_connect("***********","*********","******")))
                die( "<p>Could not connect to database</p>" );

            // open database
            if (! ($dbCon = mysql_select_db("database29", $sqlCon)))
                die( "<p>Could not open database</p>" );


       if(preg_match("/[A-Z  | a-z ]+/", $_POST ['search']))
        {
                $searchQuery = "SELECT * From USER_PROFILE WHERE FIRST_NAME LIKE '%".$search."%' OR LAST_NAME LIKE '%".$search."%' OR URL LIKE '%".$search."%' OR DESCRIPTION LIKE '%".$search."%'";
                // query database
                $searchResult= mysql_query ($searchQuery);
                if (($searchResult == false))
                {
                    user_error("Query failed: " . mysql_error() . "<br />\n$searchQuery");
                }
                elseif (mysql_num_rows($searchResult) == 0)
                {
                    echo "<p>Sorry , your request could not be found.</p>\n";
                 }
                else{
                              //Displaying fetched records to HTML table
                echo "<table border='1' id='tableResult'>";

                echo "<tr> <th>ID</th> <th>FIRST_NAME</th>
                 <th>LAST_NAME</th> <th>URL</th><th>DESCRIPTION</th></tr>";

                // Using mysql_fetch_array() to get the next row until end of table rows
                while($row = mysql_fetch_array( $searchResult )) {

                    // Print out the contents of each row into a table
                    echo "<tr><td id='#tdResult'>";
                    echo "<a href='updateQuery.php?id={$row['ID']}'>Update</a>";
                    echo "</td><td id='#tdResult'>";

                    echo $row['FIRST_NAME'];
                    echo "</td><td id='#tdResult'>";

                    echo $row['LAST_NAME'];
                    echo "</td><td id='#tdResult'>";

                    echo $row['URL'];
                    echo "</td><td id='#tdResult'>";

                    echo $row['DESCRIPTION'];
                    echo "</td></tr>";
            }
                }
            mysql_close($sqlCon);
        }

        ?>

</ul>
            </div>
        <p>
            <?php print(mysql_num_rows($searchResult))?> results was found.
        </p>
        </div>
    </div>
</body></html>

Test whether the search string is * , and then leave out the WHERE clause from the SQL.

if(preg_match('/^[A-Z |]+$|^\*$/i', $search))
{
    if ($search == '*') {
        $searchQuery = "SELECT * From USER_PROFILE";
    } else {
        $searchQuery = "SELECT * From USER_PROFILE WHERE FIRST_NAME LIKE '%".$search."%' OR LAST_NAME LIKE '%".$search."%' OR URL LIKE '%".$search."%' OR DESCRIPTION LIKE '%".$search."%'";
    }
    ...
}

BTW, did you really mean to allow | in the search string?

SELECT * 
From USER_PROFILE 
WHERE 
$search='*' OR 
(FIRST_NAME LIKE '%".$search."%' OR LAST_NAME LIKE '%".$search."%' OR URL LIKE '%".$search."%' OR DESCRIPTION LIKE '%".$search."%')
ORDER BY FIRST_NAME, LAST_NAME, URL, DESCRIPTION 

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