dongshengyin0147 2014-06-04 14:26
浏览 30

使用PHP搜索MySQL表

I am trying to create a PHP file to help search a table built in MySQL from a webpage. I have built the form, which allows the user to enter keywords into two of the search criteria and a drop-down menu for the third. However, I am having trouble with the PHP file itself. I have appeared to do something wrong and cant quite figure out what is going wrong. If anyone can spot an error in the code below I'd really appreciate the help. Thanks.

// define variables and set to empty values
$Location = $Commemorating =  "";
if (isset($_GET['Region']) && !empty($_GET['Region'])) 
{
    $Region_name = $_GET['Region'];

    if (empty($_GET["Location"])) 
    {
        $Location = "";
    } 
    else 
    {
        $Location = ($_GET["Location"]);
    }

    if (empty($_GET["Commemorating"])) 
    {
        $Commemorating = "";
    } 
    else 
    {
        $Commemorating = ($_GET["Commemorating"]);
    }

$query = "SELECT Monument,
                Location,
                Commemorating,
                Region,

        FROM MONUMENTS 
        WHERE Region = '$Region'";
        //..if a location is specified run this query
        if ($Location != "") 
        {
            $query .= " AND Location LIKE '%$Location%'";
        }
        //..and if a name is entered run this query
        if ($Commemorating != "") 
        {
            $query .= " AND Commemorating LIKE '%$Commemorating%'";
        } 
        //..and if a region is specified run this query
        if ($Region != "All") 
        {
            $query .= " AND Region LIKE '$Region'";
        }
$query_run = mysql_query($query);
}
  • 写回答

2条回答 默认 最新

  • dsx666666 2014-06-04 14:30
    关注
    $query = "SELECT Monument,
                Location,
                Commemorating,
                Region,
    

    Looks like you should strip list comma in field list from the query:

    $query = "SELECT Monument,
                Location,
                Commemorating,
                Region
    

    Like this.

    There is a bit misunderstanding since you check is Region is not empty, then query for items in given Region and then add another cause in case of Region is not 'All'. So if I run your code with Region = 'All' then the query will return only the items that have Region set to 'All', which sounds a bit odd (I'd say monuments are at a single region, isn't it?).

    You also use LIKE while may simple use = since you add sibgle quotes (') around strings so it won't give you any 'wildcard' match but slow down the query. Another thing to do is to do some mysql escape function to be sure you won't get SQL code in one of your GET query.

    May I also suggest to short your code a bit:

    $Region_name = isset($_GET['Region']) ? trim($_GET['Region']) : '';
    if ($Region_name) {
        $Location = isset($_GET['Location']) ? trim($_GET['Location']) : '';
        $Commemorating = isset($_GET['Commemorating']) ? trim($_GET['Commemorating']) : '';
        $query = sprintf("SELECT 
                        Monument,
                        Location,
                        Commemorating,
                        Region
                FROM MONUMENTS
                WHERE 1=1%s%s%s",
                        $Region!='All' ? "AND Region='".mysql_real_escape_string($Region)."'",
                        $Location ? "AND Location='".mysql_real_escape_string($Location)."'",
                        $Commemorating ? "AND Region = '".mysql_real_escape_string($Region)."'",
        );
        ...etc...
    

    I add 1=1 so I can easily add AND to the following causes without worry.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100