doulu5717 2014-04-10 06:13
浏览 20
已采纳

如果用户键入*作为关键字,则显示表中的所有条目

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 />
$searchQuery");
                }
                elseif (mysql_num_rows($searchResult) == 0)
                {
                    echo "<p>Sorry , your request could not be found.</p>
";
                 }
                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>
  • 写回答

2条回答 默认 最新

  • dougou6213 2014-04-10 06:21
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器