2016-02-28 226 views
-1

我试图创建一个搜索表单来搜索我的mysql数据库。使用php POST和mysql_fetch_数组搜索数据库

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

当我写在文本字段任何东西(包含在从数据库名称文本),并点击提交按钮没有搜索结果出现在所有。

正如你可以在代码中看到我一直在试图列出两种diferent方式的搜索结果。第一个反应如上所述。最后一个没有列出任何搜索结果。

有人知道为什么吗?我在某处有错别字还是错过代码中的任何内容?

在此先感谢。

这是我的index.php

<! DOCTYPE html> 
    <html> 
    <head> 
    <meta charset ="UTF-8"> 
    <title> Townin </title> 
    <link rel="stylesheet" type="text/css" href="Townin/style.css"> 
    <style> 
    body {background-color: white;} 

    header{background-color:#6ab47b; 
     border-color:#599a68; 
    } 


    header h1, h2 {margin-left: 20px; 
     color: white; 
     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
    } 
    h1, h1 {margin-left: 20px; 
     color: gray; 
     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
    } 
    p {margin-left: 20px; 
     color: gray; 
     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
    } 


    nav a, nav a:visited { 
     color: white; 
     background-color: #599a68;} 

    nav a.selected, nav a:hover{color: black; 
    background-color:#6ab47b;} 

    nav { 
     text-align:center; 
     padding: 0 0; 
     margin:20px 0 0; 
     height: 50 px; 
     background-color:#599a68; 
    } 

    nav ul { 
     list-style:none; 
     margin: 0px 10px; 
     padding: 0px; 
    } 

    nav ul li { 
     display: inline-block; 
     border-right: 1px solid #6ab47b; 
     text-align: center; 
     width: 250px; 
     padding:0px 0px; 
    } 

    nav ul li a { 
     display: block; 
     height: 40px; 
     width: 100%; 
     line-height:50px; 
     background-color: #599a68; 
    } 
    li a:hover { 
    background-color: #6ab47b; 

    } 
    a:link { 
     text-decoration: none; 
     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
     color:gray; 
    } 
    a:visited { 
     color:gray; 
    } 
    .button { 
     background-color:#6ab47b; 
     color: #fff; 
     text-align: center; 
     text-decoration: none; 
     display: inline-block; 
     font-size: 16px; 
     font-weight: bold; 
     margin: 0px 20px; 
     padding:20px 20px; 
     border-radius: 100 %; 
    } 
    .button:visited { 
     color:white; 
    } 
    #bar-knap { 
     display: inline-block; 
     max-width: 100%; 
     border-radius:10%; 
     height:40%; 
     width:20%; 
     margin: 10px 10% 10px 10%; 
     border-style:solid; 
     border-color:gray; 
    } 
    #spise-knap { 
     display: inline-block; 
     max-width: 100%; 
     border-radius:10%; 
     height:40%; 
     width:20%; 
     margin:10px 10% 10px 10%; 
     border-style:solid; 
     border-color:gray; 
    } 


    footer { 
     background-color: #6ab47b; 
     color: white; 
     height: 70px; 
     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
     text-align: center; 
     margin: 20px auto 0px auto; 
    } 
</style> 
</head> 
<body> 

<?php include ("Header2.php"); ?> 


<a href="barer.php" >Barer 
    <img src="img/barknap.jpg" alt="Barer" id="bar-knap"> 
</a> 

<a href="spisesteder.php" >Spisesteder 
    <img src="img/spiseknap.png" alt="Spisesteder" id="spise-knap"> 
</a> 





<!--Search form--> 
<p style="text-align:center;"> Søg mellem barer </p> 


<br /> 
<br /> 
<form method="post" action="search.php">  
<input type="text" name="search" />  
<input type="submit" name="submit" value=" Search "> 
</form> 
<br /> 
<br /> 

<footer> 
Natalie 
</footer> 


</body> 

</html> 

这是search.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> 
+2

'mysql'被弃用,使用'mysqli' –

+0

A)我建议使用mysql_函数停止。相反,使用PDO或mysqli B)我不知道其他代码,但快速查找和$ search = $ _POST [“search”];使用POST变量名称周围的引号。 – smozgur

+0

尝试回显生成的查询,并直接在数据库中运行它;它工作吗?作为一个旁白 - 牢记任何人在其中搜索带有撇号的东西都会破坏你的搜索;您应该查看使用准备好的语句,而不是将用户输入的数据直接放入查询中 – andrewsi

回答

0
  • 您需要包住你的数组键-in这个例子单quotes-如此:

    $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"; 
    

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

  • 现在,再次读取线LIKE '%$search%' OR ...如果$search是空的,那么这将返回<anyvalue><null><anyvalue> ==>%%的字符串,因此这将返回任何不在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 standardMySQL已被Deprecated从当前/未来版本的PHP中删除。它的使用并不是一个好主意,它充满了缺陷和漏洞。