用php POST和mysql_fetch_数组搜索数据库

时间:2016-02-28 22:36:58

标签: php html mysql sql

我正在尝试创建一个搜索表单来搜索我的mysql数据库。

我成功创建了一个带有一个文本字段和一个提交按钮的表单。但.. 当我将文本字段留空并点击提交按钮时,所有结果都会显示在我的search.php网站上的特定数据库表中。

当我在文本字段中写入任何内容(数据库中名称中包含的文本)并点击提交按钮时,根本不会显示搜索结果。

正如您在代码中看到的那样,我一直试图以两种不同的方式列出搜索结果。第一个如上所述作出反应。最后一个根本没有列出任何搜索结果。

有人知道为什么吗?我在某个地方有拼写错误,还是我错过了代码中的任何内容?

提前致谢。

这是我的index.php

    <?php
    //Connection to phpmyadmin
    //Step1
     $db = mysql_connect("host","username","password"); 
     if (!$db) {
     die("Database connection failed miserably: " . mysql_error());
     }

    //Step2
     $db_select = mysql_select_db("databasename",$db);
     if (!$db_select) {
     die("Database selection also failed miserably: " . mysql_error());
     }

     $search = $_POST[search];

    //SQL statement to select what to search

    $sql="SELECT * FROM Brugerdatabase
    WHERE 'Navn' like '%$search%' OR
    'Mad genre' like '%$search%' OR
    'Beliggenhed' like '%$search%'
    ORDER BY Navn ASC";


    // Run sql statement
    $result = mysql_query($sql, $db) or die(mysql_error());

    //Find out how many matches
    $number= mysql_num_rows($result);
    $pagetitle ="Search Results";



    ?>


    <!doctype html>
    <html lang="da">
    <head>
    <meta charset ="UTF-8">
    <title>Søge resultater</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <style>


table {
border-collapse: collapse;
width: 80%;
margin:0 5%;
}
table th, td {
    border-bottom:1px solid gray;
    height: 50px;
    vertical-align: center;
    padding: 15px;
    text-align: left;
 }
tr:hover{background-color:#f5f5f5}
fieldset {
    width: 60%;
    margin:0 20%; 
    box-align: center;
}

    </style>


    </head>
    <body>
  <?php include ("Header2.php"); ?>

    <!--Advanceret søge funktion-->
    <p style="text-align:center;"> Søge resultater</p>


    <?php
    //------------------------------------
    //This code inside the lines list the results when nothing is typed in the search field.
    //Creates a loop to loop through results

    while($row = mysql_fetch_array($result)){   

    echo "<table><tr><td>"
    . $row['1'] .
    "</td><td>"

    . $row['4'] .
    "</td><td>"

    . $row['5'] .
    "</td><td>"

    . $row['6'] .
    "</td><td>"

    . $row['7'] .
    "</td><td>"

    . $row['8'] .
    "</td></tr></table>";
    }
    //-----------------------------------

    //-------------------------------
    //This code inside the lines does not list anything at all..
    // loop through results and get variables

    while ($row=mysql_fetch_array($result)){
        $navn =$row["Navn"];
        $genre =$row["Mad genre"];
        $beliggenhed =$row["Beliggenhed"];
        }


    // Tabel with search results

    print " <tr>     
<td>     
    <form method=\"post\" action=\"confirmdelete.php\">  
    <input type=\"hidden\" name=\"sel_record\" value=\"$id\">    
    <input type=\"submit\" name=\"delete\" value=\"  Delete  \">
    <form>   

    <form method=\"post\" action=\"updateform.php\">
    <input type=\"hidden\" name=\"sel_record\" value=\"$id\">    
    <input type=\"submit\" name=\"update\" value=\"    Edit    \">
    </form>  
</td>    

        <td><strong>$navn</strong><br />     
            Mad genre: $genre<br />  
            Beliggenhed: $beliggenhed</td>
</tr>";

    print "</tr></table></body></html>";
   //----------------------------

    mysqli_close($db);

    ?>

    </body>
    </html>

这是search.php

<dependency>
   <groupId>edu.stanford.nlp</groupId>
   <artifactId>stanford-parser</artifactId>
   <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>edu.stanford.nlp</groupId>
    <artifactId>stanford-corenlp</artifactId>
    <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>edu.stanford.nlp</groupId>
    <artifactId>stanford-corenlp</artifactId>
    <version>3.6.0</version>
    <classifier>models</classifier>
</dependency> 

public void test(){
    MaxentTagger tagger = new MaxentTagger(MaxentTagger.DEFAULT_JAR_PATH);
    String test = "Royal Challengers Bangalore are used to making strong statements at the Indian Premier League auctions and they did so again on Saturday (February 6) with the marquee signing of seasoned Australian all-rounder Shane Watson. The staggering Rs 9.5 crore that the team paid for the 34-year-old made him the costliest buy this year.The Vijay Mallya-owned side fought off stiff competition from new entrants Rising Pune Supergiants and defending champions Mumbai Indians to snare the former Rajasthan Royals star. Watson, a battling right-handed batsman and handy medium-pacer, will add serious bite to the Virat Kohli-led Bengaluru side still chasing their maiden title.";
    String taggedString = tagger.tagString(test);
    System.out.println(taggedString);
}

1 个答案:

答案 0 :(得分:0)

  • 您需要将数组键包住 - 在此示例中使用单引号 - 所以:

    $search = $_POST['search'];

  • 您还应该使用以下方法对您的密钥进行real_escape字符串:

    $search = mysql_real_escape_string($_POST[search]);

    ......至少 (请参阅下文)

  • 您的搜索SQL不应该包含您的列名,因此请将其重写为:

    $sql="SELECT * FROM Brugerdatabase
    WHERE Navn like '%$search%' OR
    `Mad genre` like '%$search%' OR
    Beliggenhed like '%$search%'
    ORDER BY Navn ASC";
    

应该在反引号(`)中包含中间列,因为列名包含空格。引号应该只围绕值,而不是列或表名称。

  • 现在,如果LIKE '%$search%' OR ...为空,请再次阅读$search行,这将返回<anyvalue><null><anyvalue> ==&gt;的字符串%%,因此它会返回任何非NULL的列,因为它们包含某些内容。

  • 下面详细说明的代码不会向浏览器输出任何内容,因此您的查询将永远无法显示任何内容:

    //This code inside the lines does not list anything at all..
    // loop through results and get variables
    while ($row=mysql_fetch_array($result)){
        $navn =$row["Navn"];
        $genre =$row["Mad genre"];
        $beliggenhed =$row["Beliggenhed"];
        }
    
  • 最后,你真的,真的应该考虑使用MySQLi rather than the standard MySQL,因为它已经Deprecated并从当前/未来版本的PHP中删除。它的使用确实不是一个好主意,它充满了缺陷和漏洞。