【问题标题】:How to disable % from outputting everything如何禁用 % 输出所有内容
【发布时间】:2015-04-25 16:27:21
【问题描述】:

嘿,我有一个搜索字段,我正在从我的数据库中搜索某些内容,现在我在测试后发现了问题,如果我在搜索字段中输入“%”,它将输出我准备好搜索的所有内容。有没有办法禁用它?

<h3>Search Share Details</h3>
<p>You may search either by company name or issue date</p>

<form name = "search" method = "get">
<input type = "text" name = "share" size = "40" maxlength="50">
<input type = "submit" value = "Search">
</form>

获取内容连接到数据库,获取结果并打印

function get_contents() {

   if(isset($_GET['share']))
    {
        $conn = db_connect();
        $shares = get_shareSearch($conn);
        db_disconnect($conn);
        $contents = array('shares' => $shares);
        return $contents;
    }
    else
    {
        $conn = db_connect();
        $shares = get_share($conn);
        db_disconnect($conn);
        $contents = array('shares' => $shares);
        return $contents;
    }
}


function print_contents($contents) 
{

    if(count($contents['shares']) == 0)
    {
        echo "<script type = 'text/javascript'>alert('Sorry but share is not found! Q_Q');</script>";

    }
    else
    {
    ?>   
        <table>
            <tr>
                <th>Company Name</th>
                <th>Rate</th>
                <th>Issue Date</th>

            </tr>
    <?php
    foreach ($contents['shares'] as $share) 
    {
        print "<tr>";
        $identifier = urlencode($share['SHAREID']);
        print "<td><a href='share-details.php?id={$identifier}'>{$share['COMPANY']}</a></td>";
        print "<td>{$share['RATE']}</td>";

        $issue_date = $share['ISSUE_DATE'];
        $issue_date = $issue_date === NULL ? "&lt; not available &gt;" : $issue_date;
        print "<td>{$issue_date}</td>";
        print "</tr>";
    }
    ?>
        </table>
    <?php
    }
}
//require("shares.php");
require("search.php");
?>

查询自身

function get_shareSearch($conn) { 
$id = "";
if(isset($_GET['share'])){$id = $_GET['share'];}
$statement = db_create_statement($conn, "SELECT DISTINCT * FROM shares  WHERE(company LIKE '{$id}' OR issue_date LIKE '{$id}')");
$resultset = db_fetch_resultset($statement); 
return $resultset; 

}

【问题讨论】:

  • 如果我在搜索框中输入%; DROP TABLE shares,我是否会删除整个表格?
  • Escaping MySQL wild cards 的可能重复项
  • 您使用的是什么驱动程序?如果可用,请使用准备好的语句,如果没有,请考虑切换到可以使用准备好的语句的驱动程序。

标签: php html mysql search


【解决方案1】:

逃脱它

这是指在其前面放置一个字符以表示它是按字面意思理解的:

声明原文

SELECT * FROM ikeaTable WHERE chair LIKE '5% off';

转义版

SELECT * FROM ikeaTable WHERE chair LIKE '5\% off' ESCAPE '\';

你的

SELECT DISTINCT * FROM shares WHERE(company LIKE '\%{$id}' OR issue_date LIKE '\%{$id}') ESCAPE '\'

【讨论】:

  • 所以基本上我的查询就像"SELECT DISTINCT * FROM shares WHERE(company LIKE '\%{$id}' OR issue_date LIKE '\%{$id}') ESCAPE '\'" ?
【解决方案2】:

我不知道您使用的是哪个数据库库,但您当然需要转义包含在查询中的参数。如果没有转义,MySQL 会将 % 理解为一个特殊字符,基本上意味着“匹配任何东西”。

我建议您阅读数据库库文档(或代码),了解如何在语句中包含查询参数或如何直接转义它们。

【讨论】:

  • 我使用的是 My Sql,所以基本上我的查询类似于 "SELECT DISTINCT * FROM shares WHERE(company LIKE '\%{$id}' OR issue_date LIKE '\%{$id}') ESCAPE '\'" ?
  • 也许可以尝试将查询更改为“SELECT DISTINCT * FROM share WHERE company LIKE :share OR issue_date LIKE :share”,然后在代码中添加 db_bind($statement, ':share', $id );
猜你喜欢
  • 2010-10-12
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多