【问题标题】:Want to put search keywords into page URL想将搜索关键字放入页面 URL
【发布时间】:2015-06-06 08:49:49
【问题描述】:

我正在尝试将用户输入的关键字插入到搜索结果的 url 中。 我通过将 method=post 更改为 method=get 来使其工作,但它随后中断了搜索,我没有得到任何结果。我尝试过更改各种东西,但似乎没有任何效果,因此我恢复到正常工作的原始代码(减去 url 中的关键字)

搜索后的网址是:/imagesearch?go

我想成为这样的人:/imagesearch?kewords+keyword or similar.

谢谢你能给我指出正确的方向。

这是我的原始代码:

<div align="center">  
    <p>Search by keyword..</p>  
     <form  method="post" action="imagesearch?go"  id="search-form">  
       <input  type="text" name="q">  
       <input  type="submit" name="sa" value="Search">  
    </form> </div>


  if(isset($_POST['sa'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['q'])){
  $name=$_POST['q'];

  $sql="SELECT  ID FROM Image WHERE Name LIKE '%" . $name .  "%'  Order by Saleable Desc LIMIT 50";

  //-run  the query against the mysql query function

 $result=mysql_query($sql);
   $content .= ' <p><div id="wrapper">';

// Loop 

while($row=mysql_fetch_array($result)){
      $id=$row['ID'];
          $img = new Image($id);

【问题讨论】:

  • 我认为这应该是有效的 PHP?请添加编程语言标签。
  • 抱歉。现已添加相关标签,抱歉

标签: php url post get keyword


【解决方案1】:
$name=$_POST['q'];

  $sql="SELECT  ID FROM Image WHERE Name LIKE '%" . $name .  "%'  Order by Saleable Desc LIMIT 50";

您只能从 POST 变量中获得 q。这应该如何处理 GET 请求?

另外,永远不会在 SQL 查询中直接使用用户输入。想象一下q == "'; DROP TABLE Image;"Whoever has been teaching you website development should have told you before even starting explaining how you issue SQL queries.

【讨论】:

  • 感谢您指出问题 Marcus。我一直在自学,以便通过增加功能来改进我的网站。该网站尚未发布,因此将调查您提到的安全问题。谢谢
【解决方案2】:

关于你想要什么,这应该会有所帮助。

    <html>
        <form method="get">
            <input type="text" name="q">
            <button type="submit">Search</button>
        </form>
    </html>

    <?php 

        if (isset($_GET['q'])) {
            echo "<p>" . $_GET['q'] . "</p>";
        }

    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多