2016-02-28 229 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中刪除。它的使用並不是一個好主意,它充滿了缺陷和漏洞。